长按如何取消选择UITableViewCell?

时间:2019-07-16 10:21:38

标签: ios swift uitableview

我正在将UILongPressGesture添加到UITableView并长按选择单元格。

我已经用该代码编写了一些代码,可以长按选择单元格,但是现在我不明白如何长按取消选择,我向您展示我的代码以供长按选择

ViewDidLoad()中,我写了以下代码

let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress(longPressGesture:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tblListView.addGestureRecognizer(longPressGesture)

这是我的LongPress代码:

@objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {

    let p = longPressGesture.location(in: self.tblListView)
    let indexPath = self.tblListView.indexPathForRow(at: p)

    if indexPath == nil {
        print("Long press on table view, not row.")
    }
    else if (longPressGesture.state == UIGestureRecognizer.State.began) {
        print("Long press on row, at \(indexPath!.row)")
        let cell = self.tblListView.cellForRow(at: indexPath!) as! GroupDetailTableViewCell
        cell.btnDeleteMember.isHidden = false
    }
}

使用此代码,我可以选择单元格,但是现在再次长按,我想取消选择该单元格,这样我就无法理解该怎么做

请告诉我如何解决。

所以我的问题是如何长按取消选择单元格,请告诉我该怎么做

谢谢

3 个答案:

答案 0 :(得分:2)

I think you can want to hide or unhide btnDeleteMember.If so use the following code :

@objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {

    let p = longPressGesture.location(in: self.tblListView)
    let indexPath = self.tblListView.indexPathForRow(at: p)

    if indexPath == nil {
        print("Long press on table view, not row.")
    }
    else if (longPressGesture.state == UIGestureRecognizer.State.began) {
        print("Long press on row, at \(indexPath!.row)")
        let cell = self.tblListView.cellForRow(at: indexPath!) as! GroupDetailTableViewCell
        cell.btnDeleteMember.isHidden = !cell.btnDeleteMember.isHidden
    }

}

答案 1 :(得分:1)

创建全局previousIndexPath先前选择的索引路径

    //  Global variable
    var previousIndexPath : IndexPath = IndexPath()

    @objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {

        let p = longPressGesture.location(in: self.tblListView)
        let indexPath = self.tblListView.indexPathForRow(at: p)

        if indexPath == nil {
            print("Long press on table view, not row.")
        }
        else if (longPressGesture.state == UIGestureRecognizer.State.began) {
            print("Long press on row, at \(indexPath!.row)")

            // Edited - Add this to reset cell when more than one selected   
            if !previousIndexPath.isEmpty {

               // Reset the Cell
               let cell = self.tblListView.cellForRow(at: previousIndexPath!) as! GroupDetailTableViewCell
               cell.btnDeleteMember.isHidden = true

            }

            let cell = self.tblListView.cellForRow(at: indexPath!) as! GroupDetailTableViewCell
            cell.btnDeleteMember.isHidden = previousIndexPath == indexPath ? 
 true : false // This will make the Select and Deselect 
            previousIndexPath = indexPath
          }
        }

    }

答案 2 :(得分:0)

只需致电

if let selectedIndexPath = self.tblListView.indexPathForSelectedRow {
    self.tblListView.deselectRowAtIndexPath(at: selectedIndexPath, animated: true)
}

取消选择单元格。