您好,我在表格视图中添加了UILongPressGesture
,并且添加成功,但是问题是如何显示选中的单元格,我的意思是我想更改所选单元格的颜色,当我再次长按所选单元格的颜色时,我想删除单元格
我尝试用代码在我的表视图中添加长按,并在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)
因此从此代码中我可以选择单元格,但是如何向用户显示该单元格已被选中,我不知道该怎么做
所以我只想这样,当用户进行长按操作而不是更改单元格颜色并将其设置为选中状态,然后再次进行长按操作而不是取消选择具有原始颜色的单元格
答案 0 :(得分:2)
当识别longPressGesture时,只更改单元格的backgroundColor怎么办?像这样:
@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)
}
}
}
}
如果您需要澄清或我误解了某件事,请告诉我,我将编辑我的答案。