UITableViewCell中自定义cell上UILabel添加手势没有响应解决方案

来源:互联网 发布:c4d for mac下载 编辑:程序博客网 时间:2024/06/04 18:59
  • 情况一:自定义UITableViewCell,且cell中添加了一个UILabel,我们的目的是给该label添加一个手势。
  • 情况二:原生cell上添加了一个UIView,我们的目的是给UIView上的lable添加一个手势。

但是以上两种情况如果按照常规的添加方法,发现所添加的手势并不能响应。以下为解决方法:将手势添加到UITableView上或者添加到UIView上(即使添加到lable的父视图上),两种情况的解决方案类似,以下附上第一种情况的代码:(代码有点乱,还望海涵)

@interface TestViewController () <UITableViewDataSource, UITableViewDelegate>@end@implementation TestViewController {    UITableView *contentTableView;}- (void)viewDidLoad{    [super viewDidLoad];    //初始化点击手势    UITapGestureRecognizer *tagGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];    tagGesture.numberOfTapsRequired = 1;    contentTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];    contentTableView.dataSource = self;    contentTableView.delegate = self;    //给tableView添加手势操作    [contentTableView addGestureRecognizer:tagGesture];    //或者给 UIview添加手势(前提你要创建)}#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return 5;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *cellID = @"cellID";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 50, 30)];        label.tag = 1;        [cell.contentView addSubview:label];    }    UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];    label.text = [NSString stringWithFormat:@"text_%d", indexPath.row];    return cell;}#pragma mark - UITapGestureRecognizer- (void)tapGesture:(UITapGestureRecognizer *)gesture {    //获得当前手势触发的在UITableView中的坐标    CGPoint location = [gesture locationInView:contentTableView];    //获得当前坐标对应的indexPath    NSIndexPath *indexPath = [contentTableView indexPathForRowAtPoint:location];    if (indexPath) {        //通过indexpath获得对应的Cell        UITableViewCell *cell = [contentTableView cellForRowAtIndexPath:indexPath];        //获得添加到cell.contentView中的UILabel       //---------------------------------------------        UILabel *label = nil;        for (UIView *view in cell.contentView.subviews) {           //for ( UIView *view in  _perCardView.subviews)->自定义view          if ([view isKindOfClass:[UILabel class]]) {                label = (UILabel *)view;                //lable = (UILel *)[自定义view viewWithTag:2];                break;            }        }        //获得当前手势点击在UILabe中的坐标        CGPoint p = [gesture locationInView:label];        //CGPoint p = [gesture locationInView:自定义view];        //看看手势点的坐标是不是在UILabel中        if (CGRectContainsPoint(label.frame, p)) {            NSLog(@"label text : %@", label.text);        }        //---------------------------------------------    }}

注:如有错误,还望指正!

0 0