如何使用UITapGestureRecognizer调用didSelectRowAtIndexPath方法?

时间:2012-07-30 02:13:08

标签: ios xcode uitableview uitapgesturerecognizer

我在谷歌和Stackoverflow上搜索了几个小时,尝试过但没有运气。

我上面有一个UITableView tblDepartment和一个UISearchBar studentSearch

当用户在搜索框外点按时,我添加UITapGestureRecognizerstudentSearch文本框中关闭键盘:

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

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

之后,当我在didSelectRowAtIndexPath:(NSIndexPath *)indexPath中选择行时,不再调用方法tblDepartment。我知道gestureRecognizer是原因。

那么,如何隐藏键盘并仍允许用户选择行?

我尝试了这段代码但是没有用:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isDescendantOfView:tblDepartment]) {
        return NO;
    }
    return YES;
}

1 个答案:

答案 0 :(得分:14)

将手势识别器cancelsTouchesInView属性设置为NO,默认情况下为YES,如果GR识别其手势,它会阻止触摸到达基础视图。