我怎样才能添加一个UITapGestureRecognizer到表格视图单元格内的UILabel?
我正在使用NIB文件来布局自定义表格视图单元格。 这个单元格有一个名为lblName的标签。 将UITapGestureRecognizer添加到此标签永远不会触发关联的事件。 我有userInteractionEnabled = YES。
我猜测问题是,UILabel是在一个TableView和表/单元格视图拦截水龙头。 我能做点什么吗?
我想要做的就是在按下UILabel时执行一些自定义操作! 所有我所见过的解决scheme都是荒谬的。 应该很容易使用标准工具集。 但显然不是。
以下是我正在使用的代码:
- (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]; }
简单的方法:
您也可以使用该标签顶部的不可见button。 所以它会减less你为标签添加tapGesture的工作。
替代方法:
您不应该为该UILabel
创build一个IBOutlet。 当你这样做,你会在自定义类实现文件中添加一个sockets。 您无法访问其他文件。 因此,在自定义类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]; } }
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; [recognizer setNumberOfTapsRequired:1]; lblName.userInteractionEnabled = YES; [lblName addGestureRecognizer:recognizer];
这样做没有问题:
- (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创buildUILabel的情况下也是如此
您可以使用下面的代码在UITableView单元格中的UILable上添加点按手势
UITapGestureRecognizer *lblAction = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lblClick:)]; lblAction.delegate =self; lblAction.numberOfTapsRequired = 1; cell.lbl.userInteractionEnabled = YES; [cell.lbl addGestureRecognizer:lblAction];
并访问select器方法
- (void)lblClick:(UITapGestureRecognizer *)tapGesture { UILabel *label = (UILabel *)tapGesture.view; NSLog(@"Lable tag is ::%ld",(long)label.tag); }
对于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)") }
一旦将触击手势分配给UILabel并将用户交互设置为启用,在callback函数中,您可以从单元格视图中find索引path,但在超级视图嵌套中search:
- (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; }
Dineshbuild议的方式在没有for循环的情况下使用属性variables。
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; [tap setNumberOfTapsRequired:1]; [self.myUILabel addGestureRecognizer:tap];
你可以在你的单元格的-awakeFromNib方法中添加下一个
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizerAction:)]; [self.yourLabel setUserInteractionEnabled:YES]; [self.yourLabel addGestureRecognizer:gesture];
对于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. }
对于Swift 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)") }