我正在将 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)
这是我的长按代码:
@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
}
}
使用这段代码,我可以选择单元格,但现在再次长按,我想取消选择单元格,所以我无法理解该怎么做
请告诉我如何解决这个问题。
所以我的问题是如何在长按时取消选择单元格,请告诉我该怎么做
谢谢
最佳答案
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
}
}
关于ios - 如何在长按时取消选择 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57055090/