简体   繁体   English

如何避免在任何单元格中添加多个点击手势?

[英]How to avoid adding more than one tap gesture to any cell?

I have a weird problem. 我有一个奇怪的问题。 When scrolling down, cells disappear if Tap Gesture happened. 向下滚动时,如果发生“打手势”,则单元会消失。

Looks like I need to stop adding Tap Gesture to cells. 看来我需要停止向单元格中添加Tap Gesture。 I've done testing of this condition in function but it didn't work. 我已经在功能上测试了这种情况,但是没有用。

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

 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as!  ToDoItemsCell

...

        cell.textField.delegate = self
        cell.textField.isHidden = true
        cell.toDoItemLabel.isUserInteractionEnabled = true

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toDoItemLabelTapped))

        tapGesture.numberOfTapsRequired = 1
        cell.addGestureRecognizer(tapGesture)

        return cell
}

And here is my function: 这是我的功能:


 @objc func toDoItemLabelTapped(_ gesture: UITapGestureRecognizer) {

        if gesture.state == .ended {

            let location = gesture.location(in: self.tableView)

             if let indexPath = tableView.indexPathForRow(at: location) {

                if let cell = self.tableView.cellForRow(at: indexPath) as? ToDoItemsCell {
                cell.toDoItemLabel.isHidden = true
                cell.textField.isHidden = false
                cell.textField.becomeFirstResponder()
                cell.textField.text = cell.toDoItemLabel.text

           }
         }
       }
     }

Tapping works, but it keeps adding to other cells and makes them disappear. 窃听有效,但它会不断添加到其他单元格中并使其消失。 What can be the issue? 可能是什么问题?

Gesture should be added once to each cell. 手势应添加到每个单元格一次。 In your code gesture will be added every time cellForRowAt will be called and it will be called many times especially when you scroll down to list. 每次调用cellForRowAt时,都会在代码中添加手势,并且它将多次调用,尤其是当您向下滚动到列表时。

Move you gesture add code to ToDoItemsCell class and than you can use delegates to inform your view controller when cell gets tapped. 将手势添加代码移动到ToDoItemsCell类,然后您可以使用委托在单元被点击时通知视图控制器。

protocol ToDoItemsCellDelegate {
    toDoItemsCellDidTapped(_ cell: ToDoItemsCell)
}

class ToDoItemsCell : UITableViewCell {
    weak var delegate: ToDoItemsCellDelegate?
    var indexPath: IndexPath!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // code common to all your cells goes here
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toDoItemLabelTapped))

        tapGesture.numberOfTapsRequired = 1
        self.addGestureRecognizer(tapGesture)
    }

    @objc func toDoItemLabelTapped(_ gesture: UITapGestureRecognizer) {
         delegate?.toDoItemsCellDidTapped(self)
    }
}

In function cellForRowAt you can just select the delegate and set indexPath. cellForRowAt函数中,您只需选择委托并设置indexPath。

Note: If you just wanted to perform action when user taps any cell you can use didSelectRowAt method of UITableViewDelegate. 注意:如果您只想在用户点击任意单元格时执行操作,则可以使用UITableViewDelegate的didSelectRowAt方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM