Brett
Brett

Reputation: 12007

Selecting a subview in a custom UITableViewCell in UITableView

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

Upvotes: 0

Views: 1388

Answers (1)

Douwe Maan
Douwe Maan

Reputation: 6878

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;
}

Upvotes: 4

Related Questions