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?
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")
}
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")
} }
Have you considered using a UIButton
instead of an image view? You can remove the default text and replace it with the profile image in the storyboard, or programmatically button.setImage(image, for: .normal)
. This way you'll be able to connect the button to you IBAction
in your custom cell class to the touchUpInside event of the button.
UPDATE
//initialize UI Profile Image
let profileImageButton = cell?.viewWithTag(3) as! UIButton
//Make Porfile Image Button Cirlce
profileImageButton.layer.cornerRadius = profileImageButton.frame.size.width/2
profileImageButton.clipsToBounds = true
profileImageButton.addTarget(self, selector: #selector(userPhotoTaped), action: .touchUpInside)
// ...
profileImageButton.imageView?.af_setImage(withURL: downloadURL as! URL)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.