TableViewCell

时间:2016-04-26 13:40:52

标签: ios swift uitableview uigesturerecognizer

我在桌面视图单元格上有一个长按手势(只有一行),然后打开一个弹出窗口。当我关闭此弹出窗口,然后再次点击该单元格时,它不会注册第一次点击。单元格touchesBegan会被调用,但在tableView.didSelectRowAtIndexPath中,第一次点按即不会被调用。

这可能是什么原因?

我试过了:

  • 表视图的手势识别器失败
  • 重新加载表格视图
  • 使用识别器的委托方法
  • 使用按钮打开弹出窗口没有此问题

摘要:

  • 在UITableViewCell上使用UILongPressGestureRecognizer打开弹出窗口
  • 在取消弹出窗口后,点击表格视图上的第一次
    • UITableViewCell touchesBegan被称为
    • 未调用tableView.didSelectRowAtIndexPath

代码:

class CustomCell: UITableViewCell {

    weak var delegate: VC1Delegate? { didSet { addGestureRecognizer(detailsGesture) } }

    lazy var detailsGesture: gesture = {
        let gesture = UILongPressGestureRecognizer(target: self.delegate, action: #selector(VC1.openPopup)) 
        gesture.delegate = self
        return gesture
    }()

    // Setup of the view cell
}

class VC1: UIViewController, UITableViewDataSource, UITableViewDelegate, VC1Delegate {

    func openPopup(recognizer: UILongPressGestureRecognizer) {
        if recognizer.state == .Began {
            // Open popup
        }
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("cell tapped")
    }
}

经过无休止的尝试和测试后发现问题是由以下原因导致的:

tableView.scrollEnabled = false

知道为何禁用滚动导致此问题?

另一个"修复"是通过打电话而不是称之为超级:

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { }

但我不确定我是否应该这样做。

0 个答案:

没有答案
相关问题