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

后端 未结 10 1434
萌比男神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;
    }
    

提交回复
热议问题