uitapgesturerecognizer - IOS - 如何在表视图单元格的UILabel中添加UITapGestureRecognizer?

我正在使用NIB文件来布局自定义表视图单元格,我设置了userInteractionEnabled = YES。

我可以做点什么?

我要做的就是在按下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];


}



时间: 作者:

简单的方法:

你还可以在该标签的顶部使用不可见按钮,

替代方法:

您不应该为该UILabel创建IBOutlet。执行此操作时,将在自定义类实现文件中添加一个outlet ,无法访问其他文件,因此,在自定义类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创建UILabel的情况下

作者:

可以使用以下代码在UILable的UITableView单元格中添加tap手势


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);


}



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)")


}



作者:

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


UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizerAction:)];


[self.yourLabel setUserInteractionEnabled:YES];


[self.yourLabel addGestureRecognizer:gesture];



作者:

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


- (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;


}



作者:

用于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)")


}



作者:

要检测单元格中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



向视图控制器添加处理tap的方法:


@objc func labelTap(tapGesture:UITapGestureRecognizer){


 print("Label tag is:(tapGesture.view!.tag)")


}



作者:

不使用for循环时使用属性变量。


UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];


[tap setNumberOfTapsRequired:1];


[self.myUILabel addGestureRecognizer:tap];



作者:

对于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.


}



作者:
...