views:

83

answers:

1

Hi All, I have a UITableView comprised of custom UITableViewCells with some text and a button in them. The cell's allowSelection property is set to NO so that I can tap the button without selecting the cell.

I am trying to find a way to know which cell in the table was tapped when the button for that cell is pressed. Is there any way to do this???

Many thanks, Brett

+1  A: 

Use this if your UIButton is a direct subview of the UITableViewCell ([cell addSubview:button]; or cell.accessoryView = button;)

- (IBAction)buttonTapped:(id)sender {
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview;
}

Or if your UIButton is a subview of the UITableViewCell's contentView ([cell.contentView addSubview:button];):

- (IBAction)buttonTapped:(id)sender {
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
}
Douwe Maan
How could this tell me which cell the tapped button was in though? Ie., the 3rd cell in the second section. Also, my button is a subview of the UITableViewCell's contentView.
Brett
This gives you the cell itself, the `UITableViewCell` object. If you want to know the `indexPath` for this cell, however, you can use `NSIndexPath *indexPath = [someTableView indexPathForCell:cell];`
Douwe Maan
I'll give it a shot! Many thanks!
Brett
Tried it, and it works like a charm! Thanks a bunch!!!
Brett
Glad you got it working :)
Douwe Maan