Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使tableViewCell同时处理点击和长按?_Ios_Swift_Swift2_Uigesturerecognizer - Fatal编程技术网

Ios 如何使tableViewCell同时处理点击和长按?

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

我把这个放在cellForRowAtIndexPath里了

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:)")
                }
            }


        }