Reputation: 2930
I am working on adding a custom accessory view, (a button) to a UITableViewCell, and I need it to tell the table view when it is touched, but I can't figure out how to communicate to the table view what button was pressed. Ideally I'd like to somehow call a function like this:
[controller tableView:view didSelectCustomButtonAtIndexPath:indexPath usingCell:self];
when my custom view button is pressed.
Sorry if this is a bit vague, I'm not really sure how to explain this well. I am basically looking for how to mimic the implementation for tableView:didSelectRowAtIndexPath: without having to subclass UITableViewCell.
Thanks for any help.
Upvotes: 1
Views: 675
Reputation: 12613
When I had to do this, I did it like so:
//when adding accessoryView to cell in cellForRowAtIndexPath:
UIButton *b = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[b addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = b;
//the handling method
- (void)doAction:(id)sender {
NSIndexPath *indexPath = [table indexPathForCell:(UITableViewCell *)[sender superview]];
// code goes here, indexPath tells you what row was touched
}
Should work with a control other than a UIButton.
Upvotes: 3