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

  • 情况一:自定义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);
        }
        //---------------------------------------------
    }

}

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值