问题描述
我已经编写了自己的函数来在键盘出现时向上滚动文本字段.为了通过点击文本字段来关闭键盘,我创建了一个 UITapGestureRecognizer
,它负责在点击文本字段时退出文本字段的第一响应者.
I have written my own function to scroll text fields up when the keyboard shows up. In order to dismiss the keyboard by tapping away from the text field, I've created a UITapGestureRecognizer
that takes care of resigning first responder on the text field when tapping away.
现在我还为文本字段创建了一个自动完成功能,它在文本字段正下方创建一个 UITableView
并在用户输入文本时用项目填充它.
Now I've also created an autocomplete for the textfield that creates a UITableView
just below the text field and populates it with items as the user enters text.
However, when selecting one of the entries in the auto completed table, didSelectRowAtIndexPath
does not get called.相反,似乎点击手势识别器正在被调用并且只是让第一响应者辞职.
However, when selecting one of the entries in the auto completed table, didSelectRowAtIndexPath
does not get called. Instead, it seems that the tap gesture recognizer is getting called and just resigns first responder.
我猜有一些方法可以告诉点击手势识别器继续将点击消息传递给 UITableView
,但我不知道它是什么.任何帮助将不胜感激.
I'm guessing there's some way to tell the tap gesture recognizer to keep passing the tap message on down to the UITableView
, but I can't figure out what it is. Any help would be very appreciated.
推荐答案
好的,通过手势识别器文档搜索后终于找到了.
Ok, finally found it after some searching through gesture recognizer docs.
解决方案是实现 UIGestureRecognizerDelegate
并添加以下内容:
The solution was to implement UIGestureRecognizerDelegate
and add the following:
#pragma mark UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:autocompleteTableView]) {
// Don't let selections of auto-complete entries fire the
// gesture recognizer
return NO;
}
return YES;
}
这就解决了.希望这也能帮助其他人.
That took care of it. Hopefully this will help others as well.
这篇关于UITapGestureRecognizer 打破了 UITableView didSelectRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!