23

我正在使用 NIB 文件来布局自定义表格视图单元格。这个单元格有一个名为 lblName 的带有出口的标签。向此标签添加 UITapGestureRecognizer 永远不会触发关联的事件。我有 userInteractionEnabled = YES。

我猜问题是 UILabel 在 TableView 中,并且表格/单元格视图正在拦截水龙头。我可以对此做些什么吗?

我要做的就是在按下 UILabel 时执行一些自定义操作!我见过的所有这样做的解决方案都是荒谬的。使用标准工具集应该很容易。但显然不是。

这是我正在使用的代码:

- (void)tapAction {
    NSLog(@"Tap action");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; 
    [recognizer setNumberOfTapsRequired:1];
    //lblName.userInteractionEnabled = true;  (setting this in Interface Builder)
    [lblName addGestureRecognizer:recognizer];
}
4

10 回答 10

23

简单的方法:

您也可以使用该标签顶部的隐形按钮。因此,它将减少您为该标签添加 tapGesture 的工作。

替代方式:

您不应该为此创建 IBOutlet UILabel。当您这样做时,您将在自定义类实现文件中添加一个出口。您无法在其他文件中访问。因此,在自定义类中为该标签设置标签并在方法IB中编写代码。cellForRowAtIndexPath:

更新:

cellForRowAtIndexPath:方法上,

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];
    }
}
于 2012-04-10T10:43:18.743 回答
14
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; 
[recognizer setNumberOfTapsRequired:1];
lblName.userInteractionEnabled = YES;  
[lblName addGestureRecognizer:recognizer];
于 2013-07-24T09:32:02.440 回答
10

这样做没有问题:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   ...
   // create you cell
   UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
   [lbl setText:@"example"];
   [lbl setUserInteractionEnabled:YES];
   [cell.contentView addSubview:lbl];
   UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(tapAction:)];
   tap.tag = [NSIndexPath row];
   [tap setNumberOfTapsRequired:1];
   [lbl addGestureRecognizer:tap];
   ... 
}

- (void)tapAction:(id)sender {
  switch(((UITapGestureRecognizer *)sender).view.tag) {
     case 0:
          // code
          break;
     case 1:
         // code
         break;
      ....
     }
}

即使在使用 IB 创建 UILabel 的情况下

于 2012-04-09T20:16:07.663 回答
7

您可以使用下面的代码在 UITableView 单元格中的 UILable 上添加点击手势

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

cell.lbl.userInteractionEnabled = YES;
[cell.lbl addGestureRecognizer:tapGeature];

并访问选择器方法

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

对于斯威夫特

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)")
}
于 2015-10-02T07:29:49.360 回答
4

基于 Hardik Thakkar 的解决方案,使用 Swift 5 更新 2019 年。要检测单元格中 UIlabel 上的点击,请在下面的视图控制器中找到 cellForRowAt 方法。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {}

在返回单元格之前,将以下代码放入上述方法中:

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

向视图控制器添加一个方法来处理点击:

@objc func labelTap(tapGesture:UITapGestureRecognizer){
    print("Label tag is:\(tapGesture.view!.tag)")
}
于 2019-12-11T09:27:56.267 回答
1

您可以在单元格的 -awakeFromNib 方法中添加下一个

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizerAction:)];
[self.yourLabel setUserInteractionEnabled:YES];
[self.yourLabel addGestureRecognizer:gesture];
于 2014-07-18T08:31:27.567 回答
1

一旦您将点击手势分配给 UILabel 并将用户交互设置为启用,在您的回调函数中,您可以从单元格视图中找到索引路径,但搜索超级视图的嵌套:

- (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;
}
于 2014-10-20T16:46:43.370 回答
1

对于斯威夫特 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)

进而

func lblClick(tapGesture:UITapGestureRecognizer){
    print("Lable tag is:\(tapGesture.view!.tag)")
}
于 2017-08-24T00:14:23.850 回答
0

Dinesh 建议的方式将在没有使用属性变量的 for 循环的情况下工作。

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
[tap setNumberOfTapsRequired:1];
[self.myUILabel addGestureRecognizer:tap];
于 2013-07-11T17:28:36.617 回答
0

对于 Swift,您可以在 cellForRowAtIndexPath 方法中添加它。

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

然后行动

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

    // Do whatever you want.
}
于 2015-05-09T13:41:47.390 回答