tableView is hiding due to a gestureRecognizer before it can execute didSelectRowAtIndexPath

倾然丶 夕夏残阳落幕 提交于 2019-12-10 11:37:11

问题


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!


回答1:


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!




回答2:


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;
     } 
}



回答3:


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;
}


来源:https://stackoverflow.com/questions/21517559/tableview-is-hiding-due-to-a-gesturerecognizer-before-it-can-execute-didselectro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!