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

后端 未结 10 1541
萌比男神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:42

    For Swift, you can add this inside your cellForRowAtIndexPath method.

    var tap = UITapGestureRecognizer(target: self, action: "labelTapped")
    tap.numberOfTapsRequired = 1
    cell.label.addGestureRecognizer(tap)
    cell.label.tag = indexPath.row
    

    Then for action

    func labelTapped(gesture: UITapGestureRecognizer) {
        let indexPath = NSIndexPath(forRow: gesture.view!.tag, inSection: 0)
        let cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
    
        // Do whatever you want.
    }
    

提交回复
热议问题