只需双击UITableView Swift 3.0



我希望我的表视图只对双击做出反应,而对单击完全没有反应。我目前正在使用以下代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(singleTap))
tapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(tapGesture)
let doubleTapGesture = UITapGestureRecognizer()
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)
tapGesture.require(toFail: doubleTapGesture)
// implement what to do
if userInfo[indexPath.row].identifier == "username" {
editUsername()
}
}
func singleTap() {
// DO NOTHING
}

所以基本上我一直在尝试将单击"重定向"到一个什么都不做的功能。但是,我发现(在模拟器中(,tableView 有时会对单击做出反应,有时不会。非常感谢任何解决此问题的帮助!

要实现您的目标:

  1. 在表格视图上添加点击手势识别器,不要忘记设置点击数量所需 = 2
  2. 不要实现 didSelectRowAtIndexPath 方法
  3. 要防止表格视图单元格在
  4. 单击后更改其背景颜色,请在界面构建器的"属性检查器"选项卡中将表格视图"选择"属性设置为"无选择"或表格视图单元格"选择"属性设置为"无"。

如果要获取被双击的单元格的索引路径,请在手势识别器处理程序方法中获取tap.view中的点击位置,并使用tableView的indexPathForRowAtPoint方法:

let tapLocationPoint = tap.location(in: tap.view) 
let tappedCellIndexPath = tableView.indexPathForRow(at: tapLocationPoint)

最新更新