Question

I am trying to handle tableViewCell's being tapped, but the problem is that this is a "temporary tableView". I have it coded so that it will appear while the user is editing a UITextField, but then I set up a gesture recognizer to set the tableview to hidden as soon as the user clicks somewhere away from the UITextField.

I have the gesture recognizer set up as follows:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];

[tap setCancelsTouchesInView:NO];
[self.view addGestureRecognizer:tap];

However, dismissKeyboard is called before didSelectRowAtIndexPath is called, and so the TableView that I want to handle the event on becomes hidden and therefore this function is never called.

My question is: Does anybody have ideas of how to get around this, so that didSelectRowAtIndexPath will execute before the tableView hides? I had one idea to somehow see if the tableView is where the tap is coming from, and if so, then don't execute the "hide tableView" line within dismissKeyboard. Is this possible?

Sorry, but I am new to iOS dev, so thank you for any advice!

Was it helpful?

Solution

You should be able to do this by making your view controller the tap gesture's delegate and denying it any touches that are inside the table view. Here is a starting point:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch
{
    //Assuming your table view is a direct subview of the gesture recognizer's view
    BOOL isInsideTableView = CGRectContainsPoint(tableView.frame, [touch locationInView:gesture.view])
    if (isInsideTableView)
        return NO;

    return YES;
}

Hope this helps!

OTHER TIPS

You could set yourself as a delegate to the UITapGestureRecognizer and cancel the gesture when the user taps within the tableView.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
     //You can also (and should) check to make sure the gestureRecognizer is the tapGestureRecognizer    
     if (touch.view == tableView)
     {
        return NO;
     }
     else
     {
        return YES;
     } 
}

To better fit what you need, judge if your search bar is first responder.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch
{

  BOOL isInsideTableView = CGRectContainsPoint(yourTabelView.frame, [touch     locationInView:gesture.view]);

  if (isInsideTableView && ![yourSearchBar isFirstResponder])

      return NO;

  return YES;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top