如何让 uitableViewCell 同时处理点击和长按?

How to make tableViewCell handle both tap and longPress?

我把这个放在 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

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

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

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

结果 indexPathForSelectedRow returns 为零,但我做了 select 行,而且我的代码中没有任何地方 "deselectRowAtIndexPath"。

不要将 UILongPressGestureRecognizer 添加到 Cell。将其添加到 viewDidLoad

中的 UITableView
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:)))
tableView.addGestureRecognizer(longPress)

通过

获取触摸的单元格索引
@objc private func handleLongPress(sender: UILongPressGestureRecognizer) {
    if sender.state == .began {
        let touchPoint = sender.location(in: tableView)
        if let indexPath = tableView.indexPathForRow(at: touchPoint) {
            // your code here, get the row for the indexPath or do whatever you want
        }
    }
}

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

更新 Swift4:

在 viewController class 的 viewDidLoad 中添加这些行(在此示例中 class 的名称是 YourViewController

override func viewDidLoad() {
    super.viewDidLoad()

    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress(longPressGestureRecognizer:)))
    self.view.addGestureRecognizer(longPressRecognizer)
}

现在将此 func 添加到您的 viewController class

@objc func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {

    if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
        let touchPoint = longPressGestureRecognizer.location(in: self.view)
        if let indexPath = tableView.indexPathForRow(at: touchPoint) {
            // add your code here
            // you can use 'indexPath' to find out which row is selected
        }
    }
}

根据 Bala 答案是 swift 4 或 5

override func viewDidLoad() {
        super.viewDidLoad()
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longpress))
        tableView.addGestureRecognizer(longPress)
    }

方法如下

@objc func longPress(sender: UILongPressGestureRecognizer) {

            if sender.state == UIGestureRecognizer.State.began {
                let touchPoint = sender.location(in: tableView)
                if let indexPath = tableView.indexPathForRow(at: touchPoint) {
                    // your code here, get the row for the indexPath or do whatever you want
                    print("Long press Pressed:)")
                }
            }


        }