gpt4 book ai didi

swift - 使用 UITapGestureRecognizer、UIImageView 和 UITableViewCell 将参数传递给 Swift 中的选择器

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:49 25 4
gpt4 key购买 nike

我需要识别用户在 TableCell 上单击的 image

如何传递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
}
}

最佳答案

您可以使用 UIGestureRecognizerview 属性。

注册点击手势:

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 中的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36307542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com