AddGestureRecognizer not working for UILabel in tableViewCell

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



AddGestureRecognizer not working for UILabel in tableViewCell



I am trying to add tap event in UILabel inside tableView cell as below.


UILabel


tableView


let tap = UITapGestureRecognizer(target: self, action: #selector(downloadFile(_:)))
let msgLabel = cell.viewWithTag(1000) as! UILabel
msgLabel.isUserInteractionEnabled = true
msgLabel.addGestureRecognizer(tap)



This is my downloadFile function.


downloadFile


@objc func downloadFile(_ sender:Any)
print("tapped")



Can anyone solve this problem for me.





Where is this code written? Can you add the code for the full tableViewCell?
– Rakesha Shastri
Aug 13 at 7:36





It was written in tableView cellForRowAt everything is working fine. I can connect these UILabel inside tableViewCell with my model from API. The problem here is I can't get tap event for that msgLabel.
– Min Khant Lu
Aug 13 at 7:40





Are you using custom cells ?
– vivekDas
Aug 13 at 7:58





let cell = tableView.dequeueReusableCell(withIdentifier: "OwnMsg", for: indexPath) Yes, I am using cell like that. @vivekDas
– Min Khant Lu
Aug 13 at 8:06






where is your downloadFile method defined ?
– vivekDas
Aug 13 at 8:09




3 Answers
3


tap.delegate =self;
tap.numberOfTapsRequired = 1;



Add the above code to UITapGestureRecognizer object. That will make it :


UITapGestureRecognizer


let tap = UITapGestureRecognizer(target: self, action: #selector(downloadFile(_:)))
tap.delegate =self;
tap.numberOfTapsRequired = 1;
let msgLabel = cell.viewWithTag(1000) as! UILabel
msgLabel.isUserInteractionEnabled = true
msgLabel.addGestureRecognizer(tap)





Thanks for suggestion. But it is not still working.
– Min Khant Lu
Aug 13 at 7:45





@MinKhantLu I hope table view's user interaction is also enabled ?
– Rizwan
Aug 13 at 7:49






Of course.Yes. I already enabled tableView user interaction.
– Min Khant Lu
Aug 13 at 7:52



I propose an alternative by subclassing UITableViewCell. Then you create your downloadFile method inside your custom TableView Cell.



Don't forget to replace self with cell in tapGesture target:


self


cell


let tap = UITapGestureRecognizer(target: cell, action: #selector(cell.downloadFile(_:)))



I will explain more further...



First Step: Create custom cell


import UIKit

class TableViewCell: UITableViewCell

@objc func downloadFile()
print("download")


override func awakeFromNib()
super.awakeFromNib()



override func setSelected(_ selected: Bool, animated: Bool)
super.setSelected(selected, animated: animated)

// Configure the view for the selected state




Second Step: Typecast your UITableViewCell in your tableView datasource (cellForRowAt)


let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CustomTableViewCell

let tap = UITapGestureRecognizer(target: cell, action: #selector(cell.downloadFile(_:)))



Make the gesture recognizer in your UITableViewCell subclass.


class CustomTableViewCell: UITableViewCell

var exampleLabel: UILabel!

override func awakeFromNib()
super.awakeFromNib()


func initViews()

let gesture = UITapGestureRecognizer.init(target: self, action: #selector(labelTapped))
exampleLabel.addGestureRecognizer(gesture)
print(exampleLabel.frame)


@objc func labelTapped()
print("Do something here")




The above code would require you to have a cell of type CustomTableViewCell registered in your UITableView from your storyboard. If you are not using storyboard, please change the code accordingly and maybe add the initView method to didMoveToWindow override.



In your controller class, the protocol overrides should follow something of this sort:


extension HomeController: UITableViewDelegate,UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return 30


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "SomeIdentifierString", for: indexPath) as! CustomTableViewCell
return cell




Irrelevant to the actual question, but I would like to add that if you are not using storyboard, add the following line to register the UITableViewCell subclass:


let exampleTable = UITableView.init(frame: self.view.frame, style: .plain)
self.view.addSubview(exampleTable)
exampleTable.register(CustomTableViewCell.self, forCellReuseIdentifier: "SomeIdentifierString")






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard