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

den*_*330 7 uigesturerecognizer ios swift swift2

我把它放在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
Run Code Online (Sandbox Code Playgroud)

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

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

func handleTapPress(sender: UITapGestureRecognizer){
    let index = tableView.indexPathForSelectedRow!
    doSomethingElse(index)
}
Run Code Online (Sandbox Code Playgroud)

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

Bal*_*ala 13

不要添加UILongPressGestureRecognizerCell.将它添加到UITableViewviewDidLoad

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleLongPress))
yourTableView.addGestureRecognizer(longPress)
Run Code Online (Sandbox Code Playgroud)

获取触摸的细胞索引

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
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

而不是UITapGestureRecognizer使用didSelectRowAtIndexPath是一种更好的方式

  • 那么 longPressGestureRecognizer 可能是什么?您应该更改方法中的参数名称 (2认同)

Abb*_*jad 6

更新 Swift4:

将这些行添加到viewDidLoad您的 viewController 类中(在此示例中,类的名称为YourViewController

override func viewDidLoad() {
    super.viewDidLoad()

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

现在将其添加func到您的 viewController 类中

@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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


fin*_*ets 5

基于巴拉答案这里是 swift 4 或 5

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

这是方法

@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:)")
                }
            }


        }
Run Code Online (Sandbox Code Playgroud)