如何传递额外的参数

时间:2016-07-21 06:29:47

标签: ios swift uitableview parameter-passing uigesturerecognizer

我需要使用UILongPressGestureRecognizer's selector

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

我还需要发送长按的单元格。有没有办法做到这一点

提前致谢

5 个答案:

答案 0 :(得分:1)

如果该功能有两个参数,如下所示。

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

答案 1 :(得分:1)

首先更改selector UILongPressGestureRecognizer这样的语法

#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.
    }
}

答案 2 :(得分:1)

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

尝试按以下步骤操作:

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

答案 3 :(得分:0)

我认为您的意思是要将视图发送到动作功能,并且手势已添加到该视图中。

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

看起来你的动作功能目前没有根据你正在使用的选择器采取任何参数,所以你也需要纠正它。

答案 4 :(得分:0)

您需要添加

  

(_:)

#selector(didLongPressCell(_:))中的

,您的方法看起来像

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

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