Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSIndexPath of UITableViewCell subview?

Is there any was to get the NSIndexPath of a UITableViewCell's contentView subview?

In my case, I have a table view in which each row is split into 3 buttons. I can use the button tag to keep track of which column it is but I also need to know what row of the table view it is a subview of (when the user taps the button).

Because the button blocks the entire actual table view row, didSelectRowAtIndexPath is never called (which is expected), so I can't get it that way.

I have tried just [thisButton superview] but that doesn't seem to work. Any thoughts?

like image 233
Keller Avatar asked Dec 07 '11 00:12

Keller


2 Answers

I assume the button is sending its UIControlEventTouchUpInside event message to your view controller? Then you could do something like:

- (void)buttonPressed:(UIButton *)button
{
    CGPoint location = [button.superview convertPoint:button.center toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    [self doSomethingWithRowAtIndexPath:indexPath];
}
like image 135
gschandler Avatar answered Nov 02 '22 10:11

gschandler


One possible option is when making the cell for index path you can assign the tag to be 3 * row_number + <the tag number>. then just divide the tag by 3 to get the row and %3 to get the button.

like image 2
user1084563 Avatar answered Nov 02 '22 11:11

user1084563