UIView点击时通过tag传递参数

主要分两种,一种是能够自己接收点击事件的,如UIButtom,一种是本身不能接收点击事件的,如UIImageView,UILabelView
1,UIButtom的传值

cell.callBtn.tag = indexTab*1000 + indexPath.row
cell.callBtn.addTarget(self, action:  #selector(itemclicked(sender:)), for: .touchUpInside)
@objc func itemclicked(sender:UIButton) {
        print("\(sender.tag) clicked")
}

2,UIImageView的传值

cell.addImage.tag = index
let tapListen = UITapGestureRecognizer(target:self, action:#selector(itemclicked(sender:)))
cell.addImage.isUserInteractionEnabled=true
cell.addImage.addGestureRecognizer(tapListen)
@objc func itemclicked(sender:UITapGestureRecognizer) {
        print("\(sender.view?.tag) clicked")
}

对于第二个,尤其需要注意的是响应事件的参数是UITapGestureRecognizer,而UIButtom的点击事件参数是UIButtom

你可能感兴趣的:(UIView点击时通过tag传递参数)