Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to a Selector in Swift using UITapGestureRecognizer, UIImageView and UITableViewCell

I need to identify the image that the user clicked on a TableCell.

How to pass 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
}
}
like image 390
Muricy Avatar asked Mar 12 '23 18:03

Muricy


1 Answers

You can use the view property of the UIGestureRecognizer.

Register for tap gesture:

let tap = UITapGestureRecognizer(target: self, action: "tappedMe:")
imgPost1.addGestureRecognizer(tap)
imgPost1.userInteractionEnabled = true

Now define the tappedMe method

func tappedMe(sender: UITapGestureRecognizer) {
    print(sender.view?.tag)
}

PS: Don't forget to set the tag for image

like image 62
Mathews Avatar answered Apr 27 '23 20:04

Mathews