【问题标题】:How to make tableViewCell handle both tap and longPress?如何让uitableViewCell同时处理点击和长按?
【发布时间】:2016-06-12 02:44:49
【问题描述】:

我把它放在 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”。

【问题讨论】:

  • 只在 UITableViewCell 上添加长按手势,点击可以使用 UITableView 的“didSelectRowAtIndexPath”委托方法

标签: ios swift swift2 uigesturerecognizer


【解决方案1】:

不要将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 是更好的方法

【讨论】:

  • longPressGestureRecognizer 可能是什么?您应该更改方法中的参数名称
【解决方案2】:

更新 Swift4:

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

override func viewDidLoad() {
    super.viewDidLoad()

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

现在在你的 viewController 类中添加这个func

@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
        }
    }
}

【讨论】:

  • 这实际上会显示错误的 indexPath。您需要将gestureRecognizer 添加到viewDidLoad 中的tableView,而不是视图。将self.view.addGestureRecognizer(longPressRecognizer) 替换为tableView.addGestureRecognizer(longPressRecognizer)。另外,将let touchPoint = longPressGestureRecognizer.location(in: self.view) 更改为let touchPoint = longPressGestureRecognizer.location(in: tableView)
【解决方案3】:

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


        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多