UITableView没有与单个手势识别器接触

我有UITextField,UIButton和UITable视图的UIView。 文本框和button会折中search栏,然后将结果加载到表格视图中。

我想这样做,所以他们键盘驳回。 如果用户在编辑文本字段时点击某些内容。 我的策略是添加一个手势识别器的UIView,但手势识别器似乎拦截从表视图的所有触摸,你| tableView:didSelectCellAtIndexPath:| 永远不会被调用。 最有趣的是(对我来说)UIButton在用户编辑字段时仍然是可点击的,即使UITableView不是。

我试过执行| gestureRecognizer :: shouldRecognizeSimultaneouslyWithGestureRecognizer:| 总是回来是的,但这并没有帮助。 我也试过设置

singleTapRecognizer.cancelsTouchesInView = NO; 

这也没有帮助。

当文本字段调用| textFieldDidBeginEditing:|时,我很高兴添加和删除手势识别器的想法。 和| textFieldDidFinishEditing:|,虽然这感觉混乱,当你编辑文本字段(一个closures他们的键盘和删除识别器,一个点击单元格)时,仍然需要点击两个点击单元格。

有没有更好的办法?

相关代码如下:

 - (void)loadView { [super loadView]; self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.scrollView.backgroundColor = [FDEColors viewBackgroundColor]; self.view = self.scrollView; self.searchField = [[UITextField alloc] initWithFrame:CGRectZero]; self.searchField.placeholder = @"What are you looking for?"; self.searchField.backgroundColor = [FDEColors textFieldBackgroundColor]; self.searchField.clipsToBounds = YES; self.searchField.layer.borderColor = [[FDEColors buttonColor] CGColor]; self.searchField.layer.borderWidth = 1.f; self.searchField.returnKeyType = UIReturnKeySearch; self.searchField.delegate = self; [self.view addSubview:self.searchField]; self.searchButton = [[UIButton alloc] initWithFrame:CGRectZero]; self.searchButton.backgroundColor = [FDEColors buttonColor]; [self.searchButton setTitle:@"Search" forState:UIControlStateNormal]; [self.searchButton addTarget:self action:@selector(searchPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.searchButton]; self.resultsTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; self.resultsTableView.delegate = self; self.resultsTableView.dataSource = self; self.resultsTableView.backgroundColor = [FDEColors viewBackgroundColor]; [self.resultsTableView setSeparatorInset:UIEdgeInsetsZero]; self.resultsTableView.layoutMargins = UIEdgeInsetsZero; [self.resultsTableView registerClass:[FDESearchResultsCell class] forCellReuseIdentifier:[FDESearchResultsCell reuseIdentifier]]; [self.view addSubview:self.resultsTableView]; self.singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:self.singleTapRecognizer]; } - (void)dismissKeyboard { [[self view] endEditing:YES]; } 

尝试实现手势识别器的委托方法:

 - (BOOL) gestureRecognizer:ShouldReceiveRouch: 

在这种方法中,检查触摸的位置。 如果它在tableview中,则返回no,以便tableview可以接收触摸。 否则,返回YES并让识别器处理触摸。

编辑:尽pipe识别器的存在,尽pipe识别器存在触摸button接收触摸,从iOS6苹果决定给予button和其他一些控件的优先级,当涉及到手势识别。 它只适用于对抗手势,但在你的情况下一个水龙头。 例如,如果您也有一个泛识别器,识别器将优先,而不是button。

编辑2:

上面提到的方法的一个示例实现:

 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { // Determine if the touch is inside the custom subview if ([touch view] == self.yourTableView){ // If it is, prevent all of the delegate's gesture recognizers // from receiving the touch return NO; } return YES; 

}