我需要识别用户点击 image
的 TableCell
。
如何传递 TAG
?
class CustomCell: UITableViewCell {
@IBOutlet weak var imgPost1: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
imgPost1.tag=1
let tap = UITapGestureRecognizer(target: self, action: #selector(CustomCell.tappedMe))
imgPost1.addGestureRecognizer(tap)
imgPost1.userInteractionEnabled = true
}
func tappedMe(xTag:Int) {
print(xTag)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
最佳答案
您可以使用 view
的 UIGestureRecognizer
属性。
注册点击手势:
let tap = UITapGestureRecognizer(target: self, action: "tappedMe:")
imgPost1.addGestureRecognizer(tap)
imgPost1.userInteractionEnabled = true
现在定义 tappedMe 方法
func tappedMe(sender: UITapGestureRecognizer) {
print(sender.view?.tag)
}
PS:不要忘记为图片设置标签
关于swift - 使用 UITapGestureRecognizer、UIImageView 和 UITableViewCell 将参数传递给 Swift 中的 Selector,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36307542/