简体   繁体   中英

What is a good way for a custom accessory view in a UITableViewCell to message the table view controller?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM