6

I've searched for hours on Google and Stackoverflow, tried them but no luck.

I have a UITableView tblDepartment and a UISearchBar studentSearch above it.

I add a UITapGestureRecognizer to dismiss the keyboard from studentSearch textbox when users tap outside the search box:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tblDepartment addGestureRecognizer:gestureRecognizer];

- (void)hideKeyboard
{
    [studentSearch resignFirstResponder];
}

After that, the method didSelectRowAtIndexPath:(NSIndexPath *)indexPath isn't called anymore when I select row in tblDepartment. I know gestureRecognizer is the reason.

So, how can I hide the keyboard and still allow user to select row?

I tried this code but it didn't work:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isDescendantOfView:tblDepartment]) {
        return NO;
    }
    return YES;
}
Himanshu
  • 31,810
  • 31
  • 111
  • 133
Phuong Nguyen
  • 147
  • 1
  • 5
  • 13
  • Could you remove the gesture recognizer, and resign first responder from `tableView:didSelectRowAtIndexPath:` instead? – Sergey Kalinichenko Jul 30 '12 at 02:20
  • 1
    Try removing your gesture recognizer after the `resignFirstResponder` – ohr Jul 30 '12 at 02:23
  • @dasblinkenlight: Thank you for your comment. The `tblDepartment` has about 7 rows and the keyboard overlap them when display. I want users to be able to select rows if they don't want to search :) So when they tap outside the searchbox, the keyboard is dismissed and they can select the bottom rows. – Phuong Nguyen Jul 30 '12 at 02:28

1 Answers1

14

Set the gesture recognizer cancelsTouchesInView property to NO, it's YES by default, it prevents touches from reaching the underlying views if the GR recognizes its gesture.

Psycho
  • 1,863
  • 1
  • 16
  • 17