Ios 如何使tableViewCell同时处理点击和长按?
我把这个放在cellForRowAtIndexPath里了Ios 如何使tableViewCell同时处理点击和长按?,ios,swift,swift2,uigesturerecognizer,Ios,Swift,Swift2,Uigesturerecognizer,我把这个放在cellForRowAtIndexPath里了 let longPress = UILongPressGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleLongPress)) cell.addGestureRecognizer(longPress) longPress.cancelsTouchesInView = true let tapPress = UITapGes
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleLongPress))
cell.addGestureRecognizer(longPress)
longPress.cancelsTouchesInView = true
let tapPress = UITapGestureRecognizer(target: self, action: #selector(CalorieCountViewController.handleTapPress))
cell.addGestureRecognizer(tapPress)
tapPress.cancelsTouchesInView = true
然后将这些(下面的代码)放在它的外部,并完全删除了didSelectRowAtIndexPath函数,使用indexPathForSelectedRow来获取用户刚刚选择的行
func handleLongPress(sender: UILongPressGestureRecognizer){
let index = tableView.indexPathForSelectedRow!
doSomething(index)
}
func handleTapPress(sender: UITapGestureRecognizer){
let index = tableView.indexPathForSelectedRow!
doSomethingElse(index)
}
结果indexPathForSelectedRow返回nil,但我确实选择了一行,并且代码中没有“取消行索引路径”。不要将
UILongPressGestureRecognitor
添加到单元格中。将其添加到viewDidLoad
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:)))
tableView.addGestureRecognizer(longPress)
通过以下方式获取接触的单元格索引:
@objc private func handleLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == .began {
let touchPoint = sender.location(in: tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
}
}
}
使用didSelectRowAtIndexPath
而不是uitagesturerecognizer
是一种更好的方法更新Swift4:
在viewController类的viewDidLoad
中添加这些行(在本例中,类的名称是YourViewController
)
现在在viewController类中添加此func
@objc func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint = longPressGestureRecognizer.location(in: self.view)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
// add your code here
// you can use 'indexPath' to find out which row is selected
}
}
}
根据Bala,这里的答案是swift 4或5
override func viewDidLoad() {
super.viewDidLoad()
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longpress))
tableView.addGestureRecognizer(longPress)
}
这里是方法
@objc func longPress(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.began {
let touchPoint = sender.location(in: tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
print("Long press Pressed:)")
}
}
}
仅在UITableView单元格上添加长按手势,对于点击,您可以使用UITableView的“didSelectRowAtIndexPath”委托方法,以及什么可能是长按手势识别器?您应该更改Method中参数的名称,这实际上会显示错误的indexPath。您需要将手势识别器添加到viewDidLoad
中的tableView中,而不是视图中。将self.view.addgestureignizer(longPressRecognizer)
替换为tableView.addgestureignizer(longPressRecognizer)
。另外,将let touchPoint=longpressgesturecognizer.location(在:self.view中)
更改为let touchPoint=longpressgesturecognizer.location(在:tableView中)
。
@objc func longPress(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.began {
let touchPoint = sender.location(in: tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
print("Long press Pressed:)")
}
}
}