Muricy
Muricy

Reputation: 113

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
}
}

Upvotes: 4

Views: 3298

Answers (1)

Mathews
Mathews

Reputation: 743

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

Upvotes: 7

Related Questions