How can I add a UITapGestureRecognizer to a UILabel inside a table view cell?

后端 未结 10 1450
萌比男神i
萌比男神i 2021-02-04 00:43

I am using a NIB file to layout a custom table view cell. This cell has a label with outlet called lblName. Adding a UITapGestureRecognizer to this label never fires the assoc

10条回答
  •  天涯浪人
    2021-02-04 01:23

    For Swift 3

    let tapGesture : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: 
    #selector(lblClick(tapGesture:)))
    tapGesture.delegate = self
    tapGesture.numberOfTapsRequired = 1
    cell.lbl.isUserInteractionEnabled = true
    cell.lbl.tag = indexPath.row
    cell.lbl.addGestureRecognizer(tapGesture)
    

    And then

    func lblClick(tapGesture:UITapGestureRecognizer){
        print("Lable tag is:\(tapGesture.view!.tag)")
    }
    

提交回复
热议问题