简体   繁体   中英

How deselect UITableViewCell on long press?

I am adding UILongPressGesture to UITableView and select cell on long press.

I have written some code with that code I'm able to select the cell on the long-press but now I am not able to understand that how to deselect on long-press I show you my code for select on Long Press

In ViewDidLoad() I have write below code

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)

Here is my LongPress 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 = false
    }
}

With this code I am able to select cell but now again on long-press, I want to deselect the cell so I am not able to understand how to do that

Please tell me how to solve this.

So that my problem that how to deselect cell on long-press please tell me how to do that

Thanks

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
    }

}

Make a Global previousIndexPath Previous selected Index path

    //  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
          }
        }

    }

Just call

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

to deselect cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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