-1

I'm trying to add tap event listener on an UIImageView inside UITableViewCell so I added an UITapGestureRecognizer on it and used this code

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
        cell.editTap.addTarget(self, action: #selector(ProfileTVC.editTapped(sender:))) 
        //editTap is an UITapGestureRecognizer
        ...
    }
}

func editTapped(sender: UITapGestureRecognizer) {
    print("tapped")
    if sender.state == UIGestureRecognizerState.ended {
        let tapLocation = sender.location(in: self.tableView)
        if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
            if fields[tapIndexPath.row].field == "languages_spoken" {

            } else if fields[tapIndexPath.row].field == "password" {

            } else {

            }
        }
    }
}

but when I'm tapping on my UIImageView the editTapped is not being called. Also UIImageView's user interaction is enabled

Amir_P
  • 8,322
  • 5
  • 43
  • 92

2 Answers2

1

You add target not gesture

  let tapPress = UITapGestureRecognizer(target: self, action: #selector(ProfileTVC. editTapped(_:)))
 cell.editTap.addGestureRecognizer(tapPress)

//

@objc func editTapped(_ gestureRecognizer:UIGestureRecognizer)
{ 


}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

You can't add UITapGestureRecognizer with "addTarget"

Change addTarget method with this one in your CellForRowAt function;

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ProfileTVC.editTapped(sender:)))
cell.editTap.addGestureRecognizer(tapGesture) //editTap should be the ImageView inside cell.
Emre Önder
  • 2,408
  • 2
  • 23
  • 73