Ios – Tap Gesture on tableViewCell Swift

iosswiftswift3uitableview

I would like to complete an action when the image in the table view cell is taped. I set a tap gesture through the storyboard but it reads the tap everytime the cell is touched.

How can I Complete a tap gesture when only the image in my table view cell is taped?

enter image description here

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return generalRoomDataArr.count // your number of cell here
    }




    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

        //Transform Data From ^ to load at the bottom
        tableView.transform = CGAffineTransform (scaleX: 1,y: -1);
        cell?.contentView.transform = CGAffineTransform (scaleX: 1,y: -1);
        cell?.accessoryView?.transform = CGAffineTransform (scaleX: 1,y: -1);

        //Set username label to display username
        let usernameLabel = cell?.viewWithTag(1) as! UILabel
        usernameLabel.text = generalRoomDataArr[indexPath.row].username


        //Set message label to display message
        let messageLabel = cell?.viewWithTag(2) as! UILabel
        messageLabel.text = generalRoomDataArr[indexPath.row].message
        messageLabel.numberOfLines = 0

        //initialize UI Profile Image
        let imageView = cell?.viewWithTag(3) as! UIImageView

        //Make Porfile Image Cirlce
        imageView.layer.cornerRadius = imageView.frame.size.width/2
        imageView.clipsToBounds = true

        //Set timeStampLabel to current time AGO
        let timeStampLabel = cell?.viewWithTag(4) as! UILabel
        timeStampLabel.text = generalRoomDataArr[indexPath.row].timeStamp
        timeStampLabel.numberOfLines = 0


        //Loading and change of Usesrs profile image on chat cell
        let userProfileChatImage = generalRoomDataArr[indexPath.row].photoURL

        //Load profile image(on cell) with URL & Alamofire Library



            let downloadURL = NSURL(string: userProfileChatImage!)
            imageView.af_setImage(withURL: downloadURL as! URL)






        // your cell coding
        return cell!
    }






    @IBAction func userPhotoTaped(_ sender: Any) {

        print("tapped")
    }








}//END CLASS

I have also tried this

let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("TappedOnImage:")))
        tapped.numberOfTapsRequired = 1
        cell?.imageView?.addGestureRecognizer(tapped)

func TappedOnImage(sender: UITapGestureRecognizer){
        print("Elltappy")
    }

Best Answer

You should use like this. Create a custom cell and must use isUserInteractionEnabled for image view and you can handle image tapped or table view did select action.

class TestCell: UITableViewCell {

@IBOutlet weak var imageV: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // You must to use interaction enabled
    imageV.isUserInteractionEnabled = true
    imageV.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:))))
}

func imageTapped(_ sender: UITapGestureRecognizer) {
    print("image tapped")
} }
Related Topic