Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView没有';无法使用单手势识别器进行触摸_Ios_Objective C_Uitableview_Uiscrollview - Fatal编程技术网

Ios UITableView没有';无法使用单手势识别器进行触摸

Ios UITableView没有';无法使用单手势识别器进行触摸,ios,objective-c,uitableview,uiscrollview,Ios,Objective C,Uitableview,Uiscrollview,我有一个UIView,带有UIExtField、UIButton和UITable视图。textfield和button在搜索栏中显示,然后将结果加载到表视图中 我想让他们解散。如果用户在编辑文本字段时点击某些内容。我的策略是在UIView中添加一个手势识别器,但是手势识别器似乎会截获来自table view的所有触摸,而您| tableView:didSelectCellAtIndexPath:|永远不会被调用。有趣的是(至少对我来说)当用户编辑字段时,UIButton仍然是可点击的,即使UIT

我有一个UIView,带有UIExtField、UIButton和UITable视图。textfield和button在搜索栏中显示,然后将结果加载到表视图中

我想让他们解散。如果用户在编辑文本字段时点击某些内容。我的策略是在UIView中添加一个手势识别器,但是手势识别器似乎会截获来自table view的所有触摸,而您| tableView:didSelectCellAtIndexPath:|永远不会被调用。有趣的是(至少对我来说)当用户编辑字段时,UIButton仍然是可点击的,即使UITableView不是

我已经尝试使用GestureRecognitor实现了| GestureRecognitor::shouldRecognitizeSimultaneous with GestureRecognitor:|以始终返回yes,但这没有帮助。我也试过设置

singleTapRecognizer.cancelsTouchesInView = NO;
这也无济于事

我很高兴在文本字段调用| textfielddibeginediting:|和| textfielddiffinishediting:|时添加和删除手势识别器,尽管这感觉很混乱,而且在编辑文本字段时仍然需要两次点击才能触摸单元格(一次是关闭键盘并删除识别器,另一次是点击单元格)

有更好的办法吗

相关代码如下:

- (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并让识别器处理触摸

编辑:至于接受触摸的按钮,尽管识别器存在,但从iOS6开始,苹果决定在识别手势时优先考虑按钮和其他一些控件。不过,它只适用于对抗性手势,在您的情况下,只需轻触一下即可。例如,如果您还有一个平移识别器,则识别器将具有优先权,而不是按钮

编辑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;

}

整洁。除了touch.view实际上在UITableViewCellContentView上之外,这项功能运行得相对较好。很方便,您无法测试UITableViewCellContentView。我从中窃取了这个问题的答案:但是这个函数应该这样做:
-(BOOL)手势识别器:(uigesturecognizer*)手势识别器应该接收touch:(UITouch*)touch{if([touch.view isdescendatoview:self.resultsTableView]| touch.view==self.resultsTableView){返回否;}返回YES;}
是的,像这样挖掘不是最优雅的解决方案,但它确实有效。请始终记住,如果他们决定更改UITableview的实现,它可能会中断。最近在一个类似的情况下,我使用了上面的函数,但将tableview的origin.y与touch进行了比较,而不是我们上面使用的。选择更适合你的情况。