简体   繁体   中英

Detect tap on empty area of UITableView

I have a UIViewController which has a UITextFiled and a UITableView below it. I am using the UITextField as search box on UITableView.

When I tap inside UITextField the virtual keyboard appears which I want to be disappeared when I tap on any area of UITableView.The keyboard should also disappear when user taps on any area other than UITableView and UITextField. If the Keyboard is not visible and if user taps on any UITableViewCell then I need to open another UIViewController.

This is how it looks like.

在此处输入图片说明

Current Implementation: I am using UITapGestureRecognizer to detect the tap location. Also I am using shouldReceiveTouch to find whether the touch was on UITableView or not. If yes then I am returning NO, which means I am ignoring the touch on UITableView. If no then I am returning YES and inside UITapGestureRecognizer handler I am dismissing the keyboard by calling

    [self.searchTextField resignFirstResponder];

Now the problem I am facing is: When my UITableView does not have enough data and it is showing only 1 or 2 cells then in that case I want the keyboard to be dismissed if I tap on empty area of UITableView, but it will not because the tap was on UITableView and shouldReceiveTouch will return NO and ignore the touch.

NOTE The reason I am implementing shouldReceiveTouch in first place is that I want didSelectRowAtIndexPath function to be called automatically when the user taps on any cell. That will be possible only when I ignore the Tapgesture on UITableView.

I tried one more method in which I was not implementing shouldReceiveTouch and on every tap I was calling UITapGestureRecognizer handler and was detecting whether the touch is inside the UITableView or not. In that case I was not getting the correct row index which was tapped on. This is how I implemented it:

-(void)dismissKeyboard:(UITapGestureRecognizer *)tap
{
     if([self.searchTextField isFirstResponder])
     {
         [self.searchTextField resignFirstResponder];
         return;
     }

     CGPoint location = [tap locationInView:self.view];

     if(touchedOutsideUITableView)
     return;

     NSIndexPath *path = [self.lotsTableView indexPathForRowAtPoint:location];

     if(path)
     {
     // user taps on existing row
     [self tableView:self.lotsTableView didSelectRowAtIndexPath:path];
     }
     else
     {
     // handle tap on empty area of UITableView
     }
}

Can somebody tell me how do i detect the tap on empty area of UITableView in my current implementation ?

What about just keeping your UITapGestureRecognizer but turning the cancelsTouchesInView to NO? default is YES

That way when you tap on any cell your didSelectRowAtIndexPath method is still called.

I think it would be easier to just implement textFieldShouldBeginEditing , and when it triggers add a UITapGestureRecognizer to your tableView. Then remove UITapGestureRecognizer as soon as the user stops editing the text field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM