嗨,我正在开发小型IOS应用程序,我想在其中显示搜索栏及其下方的表格视图。当用户单击外部时,我想在其中隐藏键盘。因此,我使用了点击识别器,但由于我的表格视图停止监听行选择。

这是我的代码

//inside view did load
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];

-(void)dismissKeyboard {
    [_searchBar resignFirstResponder];
}


但是由于这个原因,我禁用了表视图的行选择。这意味着didSelectRowAtIndexPath永远不会被调用。任何人都有解决方案。需要帮忙。谢谢 。

最佳答案

您应该实现UIGestureRecognizerDelegate并添加以下内容:

//内部视图确实加载了

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];


// UIGestureRecognizerDelegate方法

#pragma mark UIGestureRecognizerDelegate methods

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isDescendantOfView:yourTableView]) {

        // Don't let selections of auto-complete entries fire the
        // gesture recognizer
        return NO;
    }

    return YES;
}

关于ios - UITapGestureRecognizer的表格 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25780227/

10-11 15:46