touchesBegan 在静态 tableView 中未被调用

touchesBegan in static tableView not being called

我在这里阅读了几篇关于相同问题的帖子,但主要是因为我在 IOS 开发方面还很陌生,而且我使用 swift,所以无法理解它们。另外我什至找不到术语 "subclassing" 的定义,也许它只是 obj-c?

无论如何,我有一个带有静态单元格的表视图控制器,一个单元格中有一个文本字段。当用户在编辑文本字段时点击不同区域时,我需要关闭键盘。阅读此处的帖子后,我将 touchesBegan 内容更改如下:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
        self.nextResponder()?.touchesBegan(touches, withEvent: event)
        super.touchesBegan(touches, withEvent: event)
}

我仍然没有接触定义为 :

的 tableviewcontroller
class addNew: UITableViewController, UITextFieldDelegate {

您可以在 tableview 之上添加一个 tapGestureRecognizer。 将 tapGestureRecgonizer 连接到 function/method。 在该方法中,检查文本字段是否是第一响应者。如果是,则要求文本字段退出第一响应者。

我在 Swift 4 上遇到了类似的问题,只是为了使用 touchesBegan 结束编辑和隐藏键盘。一开始我找不到办法。

这两个步骤对我有用:

  1. 将手势识别器添加到 table 中的视图:

    override func viewDidLoad() {
      super.viewDidLoad()
      tableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideKeyboard)))
    } 
    
  2. 中以两种方式结束编辑
    @objc func hideKeyboard() {
      view.endEditing(true)  
      //textField.resignFirstResponder()  /* This line also worked fine for me */
    }