Swift3 在选择器操作中传递参数

Swift3 在选择器操作中传递参数,swift3,uigesturerecognizer,selector,Swift3,Uigesturerecognizer,Selector,我正在尝试创建一个长按手势识别器,该识别器具有传递参数的操作,但我遇到了以下错误: “selector”的参数未引用“@objc”方法、属性、, 或初始值设定项 到目前为止,我唯一尝试过的就是在removeDate函数的开头添加@objc,正如另一篇帖子所建议的那样,但没有成功 let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(removeDate(deleteIndex:

我正在尝试创建一个长按手势识别器,该识别器具有传递参数的操作,但我遇到了以下错误:

“selector”的参数未引用“@objc”方法、属性、, 或初始值设定项

到目前为止,我唯一尝试过的就是在removeDate函数的开头添加@objc,正如另一篇帖子所建议的那样,但没有成功

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(removeDate(deleteIndex: 3)))
            longPressRecognizer.minimumPressDuration = 1.00
            cell.addGestureRecognizer(longPressRecognizer)

func removeDate(deleteIndex: Int) {
    if deleteIndex != 0 {
        dates.remove(at: deleteIndex - 1)
    }
}

您不能通过GestureRecognitor操作传递任何其他对象,它将允许您传递唯一的UIGestureRecognitor对象,而不传递其他对象。如果你想要长按单元格的索引,那么你可以这样尝试

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(removeDate(_:)))
longPressRecognizer.minimumPressDuration = 1.00
cell.addGestureRecognizer(longPressRecognizer)
首先设置UILongPressGestureRecognitor操作,如下所示

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(removeDate(_:)))
longPressRecognizer.minimumPressDuration = 1.00
cell.addGestureRecognizer(longPressRecognizer)
现在这样设置removeDate操作

func removeDate(_ gesture: UILongPressGestureRecognizer) {
    if gesture.state == .began {
        let touchPoint = gesture.location(in: self.tableView)
        if let indexPath = self.tableView.indexPathForRow(at: touchPoint) {
            print(indexPath)
            dates.remove(at: indexPath.row)
            self.tableView.reloadData()
        }
    }
}