删除特定TableView单元格的手势

时间:2019-05-20 18:43:22

标签: ios swift uitableview uitapgesturerecognizer uipangesturerecognizer

回答了相同的问题,但所有推荐的方法均不适用于我: How to remove long gesture in UIcollectionViewCell particular cell on selection cell?

我有一个表视图,在整个表视图中添加了两个手势,UIPanGestureRecognizer和`UITapGestureRecognizer。它使某些单元格无法像我预期的那样反应灵敏,因此我想将它们都从某个单元格中删除,这可能吗?如果是,怎么办?

非常感谢您的提前帮助

1 个答案:

答案 0 :(得分:0)

好,只有一种方法可以做到:

class MyViewController: UIViewController {

    @IBOutlet private var collectionView: UICollectionView!

    private let panGesture = UIPanGestureRecognizer()
    private let tapGesture = UITapGestureRecognizer()

    override func viewDidLoad() {
        super.viewDidLoad()
        panGesture.delegate = self
        tapGesture.delegate = self

        // Code to assign Gesture Recognizer
    }
}

// You should conform your controller to UIGestureRecognizerDelegate
extension MyViewController: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        let touchLocation = touch.location(in: collectionView)
        guard let indexPath = collectionView.indexPathForItem(at: touchLocation) else {
            return true
        }
        // let's assume that you want to disable second cell (indexPath.row starts from 0)
        let disabledRow = 1
        return indexPath.row != disabledRow
    }
}

如果您发布代码,我会更好地帮助您