zoukankan      html  css  js  c++  java
  • UITableViewCell中的UILabel添加手势没有响应的解决方法

    有时候自定义UITableViewCell,且cell中添加了一个UILabel,我们的目的是给该label添加一个手势。但是如果按照常规的添加方法,发现所添加的手势并不能响应。以下为解决方法:将手势添加到UITableView上。

    @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];
    }
    
    #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 - UITableViewDelegate
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 100.0f;
    }
    
    #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) {
                if ([view isKindOfClass:[UILabel class]]) {
                    label = (UILabel *)view;
                    break;
                }
            }
    
            //获得当前手势点击在UILabe中的坐标
            CGPoint p = [gesture locationInView:label];
            //看看手势点的坐标是不是在UILabel中
            if (CGRectContainsPoint(label.frame, p)) {
                NSLog(@"label text : %@", label.text);
            }
        }
    
    }

    原文:http://kingiol.com/blog/2013/08/28/uitableview-gesture-control/

  • 相关阅读:
    CSS 透明度 设置 兼容IE FF
    Hibernate学习---第五节:普通组件和动态组件
    Ant学习---第五节:Ant_Junit介绍(基于3的版本)
    Ant学习---第四节:Ant属性的介绍
    Ant学习---第三节:使用Ant实现一个最小的项目编译
    Ant学习---第二节:Ant添加文件夹和文件夹集的使用
    Ant学习---第一节:Ant安装和简单使用
    JPA学习---第十二节:JPA中的联合主键
    JPA学习---第十一节:JPA中的多对多双向关联实体定义与注解设置及操作
    JPA学习---第十节:JPA中的一对一双向关联
  • 原文地址:https://www.cnblogs.com/benbenzhu/p/3448251.html
Copyright © 2011-2022 走看看