Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call didSelectRowAtIndexPath method with UITapGestureRecognizer?

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;
}
like image 910
Phuong Nguyen Avatar asked Jul 30 '12 02:07

Phuong Nguyen


1 Answers

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.

like image 135
Psycho Avatar answered Nov 15 '22 14:11

Psycho