Reputation: 613
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
Upvotes: 0
Views: 1324
Reputation: 378
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
}
}
Upvotes: 2
Reputation: 592
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
}
}
}
Upvotes: 1
Reputation: 1739
Just call
if let selectedIndexPath = self.tblListView.indexPathForSelectedRow {
self.tblListView.deselectRowAtIndexPath(at: selectedIndexPath, animated: true)
}
to deselect cell.
Upvotes: 0