Swift:重新激活披露指标上的点击手势

时间:2015-08-01 14:05:01

标签: swift uitableview uigesturerecognizer

我有一个Swift项目,它有一个带有多个静态单元格的表视图控制器。有些单元格有UITextFields,有些单元格有Accessory:Disclosure Indicators。我已经实现了以下Swift代码,以便在点击背景时关闭键盘:

override func viewDidLoad() {
    super.viewDidLoad()

    let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
    tapGesture.cancelsTouchesInView = true
    tableView.addGestureRecognizer(tapGesture)
}

func hideKeyboard() {
    tableView.endEditing(true)
}

这非常适合在点击背景时释放键盘,但它也删除了Disclosure Indicators的轻击手势(滑动仍可正常工作)。有没有人知道在实现这个hideKeyboard()函数后如何重新激活Disclosure Indicators单元的点击手势?

1 个答案:

答案 0 :(得分:0)

保留对手势识别器的引用,并在隐藏键盘时将其从视图中删除。

class YourController: UITableViewController {
    let tapGesture: UITapGestureRecognizer = {
      let tg = UITapGestureRecognizer(target: self, action: "hideKeyboard")
      tg.cancelsTouchesInView = true
      return tg
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.addGestureRecognizer(tapGesture)
    }

    func hideKeyboard() {
        tableView.endEditing(true)
        tableView.removeGestureRecognizer(tapGesture)
    }
}