Josh Kahane
Josh Kahane

Reputation: 17169

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.

Upvotes: 1

Views: 7188

Answers (2)

idearibosome
idearibosome

Reputation: 684

Just implement setHighlighted:animated: method on your own custom tableview cell like this.

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];

    NSLog (@"setHighlighted:%@ animated:%@", (highlighted?@"YES":@"NO"), (animated?@"YES":@"NO"));
}

Upvotes: 13

Daij-Djan
Daij-Djan

Reputation: 50119

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

Upvotes: 1

Related Questions