swift2 如何让tableViewCell同时处理tap和longPress?

7tofc5zh  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(200)

我把它放在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来获取用户刚刚选择的行。

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

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

结果indexPathForSelectedRow返回nil,但我确实选择了一行,并且在我的代码中没有任何“deselectRowAtIndexPath”。

2g32fytz

2g32fytz1#

不要将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
        }
    }
}

使用didSelectRowAtIndexPath而不是UITapGestureRecognizer是更好的方法

knpiaxh1

knpiaxh12#

更新Swift4:
将这些行添加到viewController类的viewDidLoad中(在本例中,类的名称为YourViewController

override func viewDidLoad() {
    super.viewDidLoad()

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

现在,将此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
        }
    }
}
guykilcj

guykilcj3#

根据巴拉的答案,这里是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:)")
                }
            }

        }

相关问题