I’ve been looking around to find a solution to this, but can’t seem to find one that works for me. I have a custom cell with a button inside. My problem is how do I pass the indexPath to the action method?
Right now I’m doing
[cell.showRewards addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
In my cellForRowAtIndexPath method and my method is:
-(IBAction)myAction:(id)sender{
NSIndexPath *indexPath = [self.tableView indexPathForCell:(MyCustomCell *)[sender superview]];
NSLog(@"Selected row is: %d",indexPath.row);
}
Any tips? Thanks.
While I feel setting
tagfor the button is one way to go. You might need to write code to make sure each time the cell gets reused, the appropriate tag gets updated on the button object.Instead I have a feeling this could work. Try this –
Essentially what you are doing is getting the coordinates of where the click happened with respect to your
tableView. After getting the coordinates,tableViewcan give you the indexPath by using the methodindexPathForRowAtPoint:. You are good to go after this…Voila, you have not just the
indexPathbut also the actual cell where the click happened. To get the actual data from your datasource (assuming its NSArray), you can do –[datasource objectAtIndex:[indexPath row]];