0

Each cell in my UITableView contains a custom UIButton. I can't seem to detect in which cell the currently pressed button lies. Method gets called but indexPath.row is always 0. Any suggestions?

-(IBAction)editButtonTouch:(id)sender {
   UITableViewCell *cell = (UITableViewCell *)[sender superview];
   NSIndexPath *indexPath = [[self tableView] indexPathForCell:cell];
   switch([indexPath row]) {
       case 0:
           NSLog(@"first row");
           break;
       case 1:
           NSLog(@"second row");
           break;
   }
}
oyvindhauge
  • 3,496
  • 2
  • 29
  • 45

1 Answers1

1

This:

UITableViewCell *cell = (UITableViewCell *)[sender superview];

actually needs to be

UITableViewCell *cell = (UITableViewCell *)[[[sender superview] superview] superview];

But a better way of doing this is illustrated here: Detecting which UIButton was pressed in a UITableView

Community
  • 1
  • 1
marosoaie
  • 2,352
  • 23
  • 32
  • (UITableViewCell *)[[[sender superview] superview] superview]; didn't work (indexPath still always 0). Will check out the linked thread. – oyvindhauge Oct 03 '14 at 16:48
  • ok, so i modified this answer by removing one superview call (leaving only two) and it worked! Thanks @marosoaie :) – oyvindhauge Oct 04 '14 at 13:09