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

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

    EASY WAY:

    You may also use a invisible button on the top of that label. So it will reduce your work of adding tapGesture for that label.

    ALTERNATIVE WAY:

    You should not create an IBOutlet for that UILabel. When you do that,you will add a outlet in custom class implementation file. You cannot access in other file. So set a tag for that label in custom class IB and write a code in cellForRowAtIndexPath: method.

    UPDATED:

    In cellForRowAtIndexPath: method,

    for(UIView *view in cell.contentViews.subviews) {
        if(view.tag == 1) {
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
            [tap setNumberOfTapsRequired:1];
            [view addGestureRecognizer:tap];
        }
    }
    

提交回复
热议问题