我有一个 Swift 项目,它有一个带有多个静态单元部分的 TableView Controller 。一些单元格具有 UITextFields,其他单元格具有附件:披露指示器。我实现了以下 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)
}
这对于在点击背景时释放键盘非常有用,但它也删除了披露指示器的点击手势(滑动仍然有效)。有谁知道如何在实现此 hideKeyboard() 函数后重新激活披露指示器单元格的点击手势?
最佳答案
保留对手势识别器的引用,并在隐藏键盘时将其从 View 中删除。
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)
}
}
关于Swift:重新激活披露指示器上的点击手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31762717/