如何传递一个额外的参数

How to pass an extra parameter

我需要用 UILongPressGestureRecognizer's selector

传递第二个参数
 let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell))

我也需要发送长按的单元格。有什么办法吗

提前致谢

如果函数有如下两个参数。

func clicked(sender:AnyObject,value:AnyObject)
{
}

然后

action = "clicked::"

示例:

func switchCard(card: Int, withCard card1: Int) 
{
    print(card)
}

let singleTap1 = UITapGestureRecognizer(target: self, action: "switchCard:withCard:")

只是关于 Swift 2.2 的注释。您现在可以将选择器键入

#selector(popoverSelectedCode(_:desc:)

我假设你的意思是你想将视图发送到动作函数并将手势添加到该视图。

在这种情况下,您可以从传递给动作函数的手势中获取 view

根据您使用的选择器,您的操作函数目前似乎没有采用任何参数,因此您也需要更正它。

您需要添加

(_:)

#selector(didLongPressCell(_:)) 中,你的方法看起来像

let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell(_:)))

func didLongPressCell(sender: UIView!) -> Void {
      //Your sender here is a cell
}

首先像这样更改 UILongPressGestureRecognizerselector 语法

#selector(self.didLongPressCell(_:))

现在将此 didLongPressCell 方法添加到您的 viewController

func didLongPressCell(gesture: UILongPressGestureRecognizer) {
    if (gesture.state == .Ended) {
         let point = gesture.locationInView(self.tableView)
         let indexPath = self.tableView.indexPathForRowAtPoint(point)
         let customCell = self.tableView.cellForRowAtIndexPath(indexPath) as! CustomCell
         //This is the cell that you want.
    }
}

您可以使用手势 .view 属性 来获得长按视图。

尝试如下:

func didLongPressCell(gesture:UILongPressGestureRecognizer) {
  let cell: UITableViewCell = gesture.view as! UITableViewCell
  print(cell.textLabel?.text)
  //use this cell
}