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

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

    Once you assign the tap gesture to the UILabel and set the user interaction to enabled, in your callback function you can find the indexpath from the cell view but searching through the nest of superviews:

    - (UITableViewCell *) findCellInSuperview:(UIView *)view
    {
    UITableViewCell *cell = nil;
    
        NSString *className = NSStringFromClass([[view superview] class]);
        if ([className isEqualToString:@"UITableViewCell"]) {
            cell = (UITableViewCell *)[view superview];
        } else {
            if ([view superview] != nil) {
                cell = [self findCellInSuperview:[view superview]];
            }
        }
    
    return cell;
    }
    
    0 讨论(0)
  • 2021-02-04 01:41

    You can use below code to add tap gesture on UILable in UITableView cell

    UITapGestureRecognizer *tapGeature = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lblClick:)];
    tapGeature.delegate =self;
    tapGeature.numberOfTapsRequired = 1;
    
    cell.lbl.userInteractionEnabled = YES;
    [cell.lbl addGestureRecognizer:tapGeature];
    

    and to access the selector method

    - (void)lblClick:(UITapGestureRecognizer *)tapGesture {
        UILabel *label = (UILabel *)tapGesture.view;
        NSLog(@"Lable tag is ::%ld",(long)label.tag);
    }
    

    For Swift

    let tapGesture : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(lblClick(tapGesture:)))
    tapGesture.delegate = self
    tapGesture.numberOfTapsRequired = 1
    cell.lbl.userInteractionEnabled = true
    cell.lbl.tag = indexPath.row
    cell.lbl.addGestureRecognizer(tapGesture)
    
    func lblClick(tapGesture:UITapGestureRecognizer){
       print("Lable tag is:\(tapGesture.view!.tag)")
    }
    
    0 讨论(0)
  • 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.
    }
    
    0 讨论(0)
  • 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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题