0

While using table cells, we implement buttons and associate actions using "addTarget" with the button just like below

cell.button1.addTarget(self, action: #selector(actionforbutton(_:)), for: .touchUpInside)

how we can associate other properties like labels, UIImageView or UIView of the table cell in a similar fashion to perform certain actions, as I couldn't find " addTarget " while using the labels, UIImageView or UIView

Keshu R.
  • 5,045
  • 1
  • 18
  • 38

5 Answers5

0

You can use UITapGestureRecognizer

Here is the Sample Code:

  let tap = UITapGestureRecognizer(target: self, action: #selector(actionforView(_:)))
  VIEW.addGestureRecognizer(tap)
  VIEW.isUserInteractionEnabled = true
Taimoor Suleman
  • 1,588
  • 14
  • 29
0

You can use UITapGestureRecognizer on UILabel, UIImageView etc.

@IBOutlet weak var lblStatus: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
    lblStatus.isUserInteractionEnabled = true
    lblStatus.addGestureRecognizer(tapGesture)


}

@objc func handleTap(sender: UITapGestureRecognizer) {
    guard let a = (sender.view as? UILabel)?.text else { return }
    print(a)

}
Keshu R.
  • 5,045
  • 1
  • 18
  • 38
deepak
  • 78
  • 4
0

If you want to add UITapGestureRecognizer in tableview or collectionview cell then only add target in the cellForItemAt or cellForRowAt. Allocation and initialization should be placed in awakeFromNib of the custom class of the cell! If you create UITapGestureRecognizer in cellForRowAt then it will initialize every time when cellForRowAt getting called and it is not good practice.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

I'll try to explain the approach using a customView of type UIView.

In your TableViewCell's awakeFromNib() method, add a UITapGestureRecognizer to the customView with target as self and selector as the relevant method to be called when the customView is tapped, customViewTapped(_:) in the below code.

class TableViewCell: UITableViewCell {
    @IBOutlet weak var customView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
        let gesture = UITapGestureRecognizer(target: self, action: #selector(customViewTapped(_:)))
        self.customView.addGestureRecognizer(gesture)
    }

    @objc func customViewTapped(_ gesture: UITapGestureRecognizer) {
        print("customView Tapped")
    }
}

Same thing you can do for UILabel, UIImageView and other view elements.

PGDev
  • 23,751
  • 6
  • 34
  • 88
0

What you can do actually, there are two common practice to have control on ViewCels, since those are views you can simply create and extensions to make your work time less. For example;

//This enum can be extended due to demand
enum GestureRecognizerType {
    case Tap
    case Pinch
    case Rotate
}

extension UIView {
func addRecognizer(_ gType:GestureRecognizerType, handleGesture:Selector, _ controller:UIViewController){
    var recognizer:UIGestureRecognizer!
    switch gType {
    case .Pinch:
        recognizer = UIPinchGestureRecognizer(target: controller, action: handleGesture)
    case .Rotate:
          recognizer = UIRotationGestureRecognizer(target: controller, action: handleGesture)
    case .Tap:
          recognizer = UITapGestureRecognizer(target: controller, action: handleGesture)
    }
    self.addGestureRecognizer(recognizer)
}

Then call wherever you want to. Usage could be like:

override func viewDidLoad(){
    super.viewDidLoad
    let view = UIView()
    view.addRecognizer(.Tap, handleGesture: #selector(handleTap(_:)), self)

}
@objc func handleTap(_ recognizer:UITapGestureRecognizer){
    //Handle when the view tapped
}

The other practice could be handle all inside of your custom cell Class and give them recognisers when they are set using didSet.

Mehmet Baykar
  • 367
  • 3
  • 11