views:

45

answers:

1

Hi Everyone, 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.

+2  A: 

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.

David Kanarek
I like this, very clean way of doing it. Is it possible for my custom view to add an arguement to the call of doAction:? I want possibly more than one subview in a cell, so I'm hoping to send a @selector() as a variable for this.
Alex Gosselin
From Apple's documentation "You may call this method multiple times, and you may specify multiple target-action pairs for a particular event. The action message may optionally include the sender and the event as parameters, in that order."You might be able to do it by looking at the event's touch's position, but I think it would be easier to add a target to each sub accessory.
David Kanarek