你好,我在我的表格 View 中添加了 UILongPressGesture
,我已经成功添加了,但问题是如何显示该单元格已被选中,我的意思是我想更改所选单元格的颜色,当我再次长按时选择的单元格比我想删除的单元格
我已经尝试使用代码在我的表格 View 中添加长按并在 LongPress 上分配代表,这是我的代码
@objc func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizer.State.began {
let touchPoint = longPressGestureRecognizer.location(in: self.tblList)
if let indexPath = tblList.indexPathForRow(at: touchPoint) {
}
}
}
在 viewDidload()
中,我正在编写这段代码
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(SMSChatViewController.longPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tblList.addGestureRecognizer(longPressGesture)
所以从这段代码我可以选择单元格但是如何向用户显示单元格已被选中我不知道该怎么做
所以我只是想这样当用户长按而不是单元格颜色更改并设置为选中然后再次长按而不是取消选择其原始颜色的单元格
最佳答案
当 longPressGesture 被识别时,你只改变单元格的背景颜色怎么样?像这样:
@objc func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizer.State.began {
let touchPoint = longPressGestureRecognizer.location(in: self.tblList)
if let indexPath = tblList.indexPathForRow(at: touchPoint) {
let cell = tblList.cellForRow(at: indexPath)
if (cell.isSelected) {
cell.backgroundColor = UIColor.clear // or whatever color you need as default
cell.setSelected(false, animated: true)
} else {
cell.backgroundColor = UIColor.orange
cell.setSelected(true, animated: true)
}
}
}
}
如果您需要澄清或我误解了什么,请告诉我,我会编辑我的答案。
关于ios - 如何在长按上显示用户选择的表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57111954/