UITableViewCell中的UILabel添加手势没有响应的解决方法
2013-11-28 18:09
453 查看
有时候自定义UITableViewCell,且cell中添加了一个UILabel,我们的目的是给该label添加一个手势。但是如果按照常规的添加方法,发现所添加的手势并不能响应。以下为解决方法:将手势添加到UITableView上。
原文:http://kingiol.com/blog/2013/08/28/uitableview-gesture-control/
@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/
相关文章推荐
- UITableViewCell中自定义cell上UILabel添加手势没有响应解决方案
- 自定义cell,添加uilable,并且给lable添加tap手势,不响应的问题,对应解决方法
- UITableViewCell中自定义cell添加手势没有响应解决方案
- 解决点击UITableViewCell时和自定义添加的手势冲突的问题
- 在UITableView或UICollectionView的自定义的cell里添加手势或button,响应方法时如何传参数
- ios UIImageView添加图片动画效果,添加了手势之后,依然没有反应的解决方法
- iOS解决UIcollectionView添加手势后不响应其cell的问题
- iis服务没有及时响应启动或控制请求错误产生原因及解决方法
- 手势识别(三)2种添加手势方法和如何响应手势
- IIS无法启动提示“服务没有及时响应启动或控制请求”的解决方法[图解]
- IIS无法启动提示“服务没有及时响应启动或控制请求”的解决方法[图解]
- IOS8 UITableViewCell.detailTextLabel不显示解决方法
- 启动IIS时提示“服务没有及时响应启动或控制请求”几种解决方法
- 第十三篇、Swift_Nav自定义返回按钮后或者隐藏导航栏,Pop返回手势失效的解决方法 Pop全局返回添加的方法
- 启动IIS提示“服务没有及时响应启动或控制请求”解决方法
- 启动IIS时提示“服务没有及时响应启动或控制请求”几种解决方法
- Ubuntu 16.04 使用useradd添加用户后没有家目录的解决方法
- 微信公众平台消息接口开发(2)你的服务器没有正确响应Token验证的解决方法
- 启动IIS时提示“服务没有及时响应启动或控制请求”几种解决方法
- 启动windows服务时出现“错误1053:服务没有及时响应启动或控制请求”的解决方法