Search code examples
iphoneobjective-cuitableviewtouch

Get selected UITableViewCell on touch down


I need to get the the currently selected UITableViewCell as its tapped. So upon my finger touching the screen/cell I want to run a method from which I can say something as simple as:

selectedCell = cell;

cell being the one I just tapped and selectedCell being a copy I store.

I am using a custom subclassed UITableViewCell which is why I think I'm having trouble.


Solution

  • TouchDown
    - setSelected:animated: is called on the cell itself on touch down here, you can inform a cells delegate

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
       [self.delegate willSelectCell:self];
    }
    

    declare custom cells delegate as property of the cell

    @property id<MyCellDelegate> delegate;
    

    TouchUP
    save the cell in the delegate

    - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _selectedCell = [aTableView cellForRowAtIndexPath:indexPath];
    }
    

    just pay attention to the fact that Cell Views can be reused