【问题标题】:Issue with UILongPressGestureRecognizer while using it in table view cell在表格视图单元格中使用 UILongPressGestureRecognizer 时出现问题
【发布时间】:2017-05-24 13:12:00
【问题描述】:

我正在通过 swift3 中的故事板在 uitableview 中实现长按。我在情节提要中只有一个原型单元集。但问题是仅在第一个单元格中检测到长按。其余单元格不听长按手势。

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let row = indexPath.row
        cell.textLabel?.text = "Label"

        return cell
}

    @IBAction func longPress(_ guesture: UILongPressGestureRecognizer) {

        if guesture.state == UIGestureRecognizerState.began {
            print("Long Press")
        }
    }

控制台中显示的警告是:

以前从未允许这样做,现在已强制执行。从 iOS 9.0 开始,它将被放置在它加载到的第一个视图中。

【问题讨论】:

  • 您要将手势附加到哪个视图?
  • 合适的视图单元格
  • 只需将longpressgesture添加到整个表格视图
  • @JitendraModi 谢谢我明白了....
  • 根据以下答案,每次调用方法时都会分配 logpress 手势。取而代之的是,您可以直接将手势添加到 tabelview 所以它只会启动一次。并用于所有细胞

标签: ios uitableview swift3 uilongpressgesturerecogni


【解决方案1】:

将手势附加到 tableview,并在触发手势时确定选择了哪个 indexPath。

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

func longPress(_ guesture: UILongPressGestureRecognizer) {

    if guesture.state == UIGestureRecognizerState.began {
        let point = guesture.location(in: tableView)
        let indexPath = tableView.indexPathForRow(at: point);
        print("Long Press \(String(describing: indexPath))")
    }
}

因为 tableview 是一种滚动视图,所以最好将手势附加到 tableview 本身,而不是它的任何子视图。这样就不太可能干扰必须跟踪的其他手势。

【讨论】:

    【解决方案2】:

    您需要为 cellForRowAtIndexPath 中的所有单元格添加手势

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            let row = indexPath.row
            cell.textLabel?.text = "Label"
    
            let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(HomeViewController.longPress(_:)))
            cell?.addGestureRecognizer(longPressRecognizer)
            return cell
    }
    
    
    
    func longPress(_ guesture: UILongPressGestureRecognizer) {
    
            if guesture.state == UIGestureRecognizerState.began {
                print("Long Press")
            }
        }
    

    【讨论】:

    • 为什么要投反对票?
    • 您不应为每个单元格附加手势。最好创建一个gestureRecongnizer,放到tableview上。请参阅 developer.apple.com/library/content/documentation/WindowsViews/… " UICollectionView 类是 UIScrollView 的后代,因此将手势识别器附加到集合视图不太可能干扰必须跟踪的其他手势。"
    猜你喜欢
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2012-05-10
    相关资源
    最近更新 更多