Ios 带有UITapGestureRecognitor的表视图
嗨,我正在开发一个小的IOS应用程序,我想在其中显示搜索栏和它下面的表视图。在其中,当用户单击外部时,我想隐藏键盘。由于这个原因,我使用的是点击识别器,但是由于这个原因,我的表视图停止了对行选择的监听 这是我的密码Ios 带有UITapGestureRecognitor的表视图,ios,uitableview,tap,didselectrowatindexpath,Ios,Uitableview,Tap,Didselectrowatindexpath,嗨,我正在开发一个小的IOS应用程序,我想在其中显示搜索栏和它下面的表视图。在其中,当用户单击外部时,我想隐藏键盘。由于这个原因,我使用的是点击识别器,但是由于这个原因,我的表视图停止了对行选择的监听 这是我的密码 //inside view did load UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
//inside view did load
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
-(void)dismissKeyboard {
[_searchBar resignFirstResponder];
}
但正因为如此,我的表视图行选择被禁用。这意味着,
didSelectRowAtIndexPath
永远不会被调用。任何人都有解决这个问题的办法。我需要帮助。谢谢。无需使用TapGestureRecognizer。使用SearchBarDisplayController
您应该实现UIgestureRecognitizerDelegate并添加以下内容: //内部视图没有加载
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
//UIgestureRecognitzerDelegate方法
#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;
}
尝试添加这行代码,这将解决您的问题
tap.cancelsTouchesInView = NO;
在实用程序面板中有一个“搜索栏和搜索显示控制器”,听起来它非常适合您。这是一个很好的教程,解释了如何实现它。这样你就不会再有键盘问题了