ios - 在 UITapGestureRecognizer 中传递参数

标签 ios swift

所以,我有一个包含多行的表格。

每一行都有自己的 gestureRecognizer,在这个手势识别器中,我想将相应行的 Int 作为参数传递,这样我就可以通过数据数组 data[rownum] 访问数据。

如何访问手势识别器 GoToSendEmail 上的整数?

这是我现在的代码

var x = indexPath.row
        let gestureEdit = UITapGestureRecognizer(target: self,
                                                 action: #selector(self.GoToSendEmail(_:rowNum:))
                                                )
 cell.btnEdit.addGestureRecognizer(gestureEdit)

-

@objc func GoToSendEmail(_ sender:UITapGestureRecognizer, rowNum:Int)
{
    print("rownum:" + String(rowNum)) // rownum is always 0
    // data[rowNum] // this is the information i want
}

最佳答案

如果您的 btnEdit 是一个 UIButton,您可以直接使用目标更轻松地做到这一点。

cell.btnEdit.tag = indexPath.row
cell.btnEdit.addTarget(self, action: #selector(self.editButtonClicked(_:)), for: .touchUpInside)

和函数

func editButtonClicked(_ sender: UIButton) {

   print(sender.tag) // your cell indexPath.row
   // or
   print(data[sender.tag])
}

关于ios - 在 UITapGestureRecognizer 中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44066393/

相关文章:

ios - 如何根据在 UITableViewCell 中点击的单元格进行判断?

ios - ARKIT 2.0 - ARImageTrackingConfiguration

android - coffeescript 可以与 HTML5、CSS3 一起使用来创建/开发 Android 应用程序吗?

ios - 查询 Firebase 用户以检查联系人是否存在于数据库中

ios - Swift 使用没有泛型的动态类型引用创建类型数组

ios - 获取具有最近日期的对象

xcode - Xcode Symbol Navigator 中的重复类

javascript - ontouchstart 在 iPad 上不可用?

ios - UITextField蓝色背景,突如其来

ios - SKPhysicsContactDelegate 协议(protocol)方法未被调用