0

I have an UITableViewController that contains a tableView. I have added a tap gesture recognizer to this controller and I would like the action to happen when the user clicks everywhere except for the first row of the tableView (with indexPath.row == 0).

I have seen the following related questions and answers (among others):

  • this one is concerned with detecting the tableView itself (therefore it will also detect its header / footer).

  • I has used this one (code below) before but the problem is that it computes an indexPath of (0,0) also if one clicks on the footer / header.

.

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {   
    CGPoint buttonPosition = [touch locationInView:self.view];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath.row == 0) {
        return NO; // will also be returned if I click on the header / footer
    }
    return YES;
}
Community
  • 1
  • 1
vib
  • 2,254
  • 2
  • 18
  • 36
  • Smells like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why don't you tell us what you really want to do? – Ozgur Vatansever Jan 02 '16 at 22:11
  • I want to return `YES`in all cases but when I click on a particular row (fixed in advance). The problem being that if this row is the first one I don't want to return `YES`when I click on the tableView header / footer. – vib Jan 02 '16 at 22:16

1 Answers1

1

Perhaps attempt to add an "invisible" UIButton over the table view cells and then upon tap use the buttons tag as your index. Like the pseudo code below:

-cellForRowAtIndexPath:index {
   UIButton *button = [UIButton alloc]initWithFrame:cell.frame];
   [button setTag:index.row];
   [button addTarget:self action:@selector(getIndex:) forControlEvents:UIControlEventTouchUpInside];
   [cell addSubview: button]
}


-(void)getIndex:(UIButton *)sender {
 finalIndex = [sender tag];
}
Matt Sarabyte
  • 245
  • 1
  • 3
  • 10
  • Thanks for your answer. It seems my question was actually ill-formulated. I am relly sorry about that. I will clarify what I want to do – vib Jan 03 '16 at 09:16