ios - GestureRecognizer 不适用于 TableView

标签 ios swift uitableview

我正在尝试找到一种方法,当用户在过滤我的 tableview 的内容后点击屏幕上的其他地方时,立即隐藏键盘。

但不知何故,当我添加 UITapGestureRecognizer 时,我的 TableView 卡住了,无法进一步进入应用程序。

这是我的做法:

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
self.view.addGestureRecognizer(tap)

func dismissKeyboard() {

    view.endEditing(true)
}

我已经在带有 TextField 的 Blank ViewController 上尝试过这个并且它有效。我错过了什么?有没有一种特定的方法可以将 GestureRecognizer 添加到 TableView 中?因为一旦我添加手势,它就会破坏一切。我也尝试使用 resignFirstResponder 得到相同的结果。

有什么建议吗?谢谢!

最佳答案

尝试将 cancelsTouchesInView 变量设置为 false。这默认为 true,通过将其设置为 false,您允许通过手势响应触摸,然后通过手势返回 View ,以便 tableView 也可以响应。

tap.cancelsTouchesInView = false

Documentation on Apple

文档中的讨论摘录:

When this property is true (the default) and the receiver recognizes its gesture, the touches of that gesture that are pending are not delivered to the view and previously delivered touches are cancelled through a touchesCancelled:withEvent: message sent to the view. If a gesture recognizer doesn’t recognize its gesture or if the value of this property is false, the view receives all touches in the multi-touch sequence.

关于ios - GestureRecognizer 不适用于 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358762/

相关文章:

ios - 在 Xcode 6 上的 iPad Mini Retina 上运行应用程序

ios - 带有 Realm 对象存储的 RxSwift 变量

ios - (xcode) 我想长按一个按钮导致键盘弹出

swift - ios7及更高版本导致图像图标错误

ios - 如何找到 NSMutableArray 索引

ios - NSArray 错误 : NSCFArray objectAtIndex index (5) beyond bounds (5)

ios - 滚动时未调用 sizeForItemAt

ios - 如何在带有部分的 TableView 上创建搜索栏?

ios - 在 Swift/SwiftUI 中包含多种 View 类型的数组

ios - UITableView 单元格文本标签不显示文本