如何使tableViewCell同时处理tap和longPress?

时间:2021-03-07 00:06:27

I put this in cellForRowAtIndexPath

我把它放在cellForRowAtIndexPath中

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleLongPress))
cell.addGestureRecognizer(longPress)
longPress.cancelsTouchesInView = true
let tapPress = UITapGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleTapPress))
cell.addGestureRecognizer(tapPress)
tapPress.cancelsTouchesInView = true

and put these(code below) outside of it, and removed didSelectRowAtIndexPath function entirely, use indexPathForSelectedRow instead to get which row user just selected.

并将这些(下面的代码)放在它之外,并完全删除didSelectRowAtIndexPath函数,改为使用indexPathForSelectedRow来获取刚刚选择的行用户。

func handleLongPress(sender: UILongPressGestureRecognizer){
    let index = tableView.indexPathForSelectedRow!
    doSomething(index)
}

func handleTapPress(sender: UITapGestureRecognizer){
    let index = tableView.indexPathForSelectedRow!
    doSomethingElse(index)
}

Turns out indexPathForSelectedRow returns nil, but I did select a row, and there is no "deselectRowAtIndexPath" anywhere in my code.

原来indexPathForSelectedRow返回nil,但我选择了一行,并且在我的代码中没有“deselectRowAtIndexPath”。

1 个解决方案

#1


11  

Don't add the UILongPressGestureRecognizer to Cell. Add it to UITableView in viewDidLoad

不要将UILongPressGestureRecognizer添加到Cell。将它添加到viewDidLoad中的UITableView

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleLongPress))
yourTableView.addGestureRecognizer(longPress)

Get the touched cell index by

获取触摸的细胞索引

func handleLongPress(sender: UILongPressGestureRecognizer){
    if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
         let touchPoint = longPressGestureRecognizer.locationInView(yourTableView)
         if let indexPath = yourTableView.indexPathForRowAtPoint(touchPoint) {
        // your code here, get the row for the indexPath or do whatever you want
         }
    }
}

Instead of UITapGestureRecognizer use didSelectRowAtIndexPath is a better way

使用didSelectRowAtIndexPath代替UITapGestureRecognizer是一种更好的方法

#1


11  

Don't add the UILongPressGestureRecognizer to Cell. Add it to UITableView in viewDidLoad

不要将UILongPressGestureRecognizer添加到Cell。将它添加到viewDidLoad中的UITableView

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleLongPress))
yourTableView.addGestureRecognizer(longPress)

Get the touched cell index by

获取触摸的细胞索引

func handleLongPress(sender: UILongPressGestureRecognizer){
    if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
         let touchPoint = longPressGestureRecognizer.locationInView(yourTableView)
         if let indexPath = yourTableView.indexPathForRowAtPoint(touchPoint) {
        // your code here, get the row for the indexPath or do whatever you want
         }
    }
}

Instead of UITapGestureRecognizer use didSelectRowAtIndexPath is a better way

使用didSelectRowAtIndexPath代替UITapGestureRecognizer是一种更好的方法