有时候自定义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 = ; 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 ;
} - (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(, , , )];
label.tag = ;
[cell.contentView addSubview:label];
} UILabel *label = (UILabel *)[cell.contentView viewWithTag:];
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中的UILabel添加手势没有响应的解决方法的更多相关文章

  1. JabRef中添加中文文献出现乱码 解决方法

    JabRef中添加中文文献出现乱码 解决方法     问题描述 JaBRef是一款开源的文献管理软件,主要用来管理bibtex格式的参考文献,可以与LATEX配合使用,方便论文参考文献的使用.文献管理 ...

  2. 在MonoGame中SetRenderTarget会把后备缓冲区清除的解决方法

    在MonoGame中SetRenderTarget会把后备缓冲区清除的解决方法: 在构造函数中添加事件:graphics.PreparingDeviceSettings += Graphics_Pre ...

  3. 检索 COM 类工厂中 CLSID 为 {10020200-E260-11CF-AE68-00AA004A34D5} 的组件时失败,解决方法如下:

    检索 COM 类工厂中 CLSID 为 {10020200-E260-11CF-AE68-00AA004A34D5} 的组件时失败,解决方法如下: 第 一步:首先将msvcr71.dll,  SQLD ...

  4. log4j中Spring控制台输出Debug级信息过多解决方法

    log4j中Spring控制台输出Debug级信息过多解决方法 >>>>>>>>>>>>>>>>> ...

  5. universal image loader在listview/gridview中滚动时重复加载图片的问题及解决方法

    在listview/gridview中使用UIL来display每个item的图片,当图片数量较多需要滑动滚动时会出现卡顿,而且加载过的图片再次上翻后依然会重复加载(显示设置好的加载中图片) 最近在使 ...

  6. MySQL中遇到的几种报错及其解决方法

    MySQL中遇到的几种报错及其解决方法 1.[Err] 1064 - You have an error in your SQL syntax; check the manual that corre ...

  7. [datatable]关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法

    -- :09关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法 在实际编程工程中,常常遇到这样的情况:DataTable并不 ...

  8. Eclipse中SVN修改的*星号没了,解决方法

    Eclipse中SVN修改的*星号没了,解决方法 打开Preference 第一步:去掉外加的 ">" 第二步:勾选Outgoing changes 这样做之后," ...

  9. Python3中使用HTMLTestRunner报No module named 'StringIO'解决方法

    今天在学习使用HTMLTestRunner生成测试报告时遇到一个报错,如图所示: 网上搜索了下“No module named 'StringIO'”解决方法,原来我用的是Python 3.X版本,而 ...

随机推荐

  1. mogadm修改硬盘状态

    #查看主机序列mogadm host list #查看空间快满的分区 df -h#检查硬盘序列和状态ls -l /data/mogile_data/ #查看mogadm目录下软链接的目录对应的分区 # ...

  2. ECMAScript和JavaScript的关系

    JavaScript 是一种基于 ECMAScript 规范的脚本语言,并在此基础上进行了自己的封装.ECMAScript 不是一种编程语言,仅仅是一种脚本语言规范,由欧洲计算机协会制定和发布,任何基 ...

  3. 正则表达式之Regex.Match()用法

    //匹配字符串中的连续数字 string txt = "AAA12345678AAAA"; string m = Regex.Match(txt, @"\d+" ...

  4. AC日记——[Wc2008]游览计划 bzoj 2595

    2595 思路: 状压DP+spfa转移+dfs输出路径: 或者,斯坦纳树算法模板: 来,上代码: #include <queue> #include <cstdio> #in ...

  5. UpdateLayeredWindow后,使用Gdi DrawText文字透明的解决办法

    来源:http://stackoverflow.com/questions/5309914/updatelayeredwindow-and-drawtext 要点就是在先在memDc DrawText ...

  6. (3)C#工具箱-容器

    容器特点:把控件放到容器里,移动容器控件也会跟着移动. 1.flowLayoutPanel(流布局控件) 放入控件后,会自动垂直或水平排列 拉长布局,控件自动跑到一行 2.GroupBox(组合框) ...

  7. RQNOJ PID217 / [NOIP1999]拦截导弹【n^2 / LIS】

    题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

  8. 【poj1149】 pigs 网络流最大流问题

    描述 Description 尼克在一家养猪场工作,这家养猪场共有M间锁起来的猪舍,由于猪舍的钥匙都给了客户,所以尼克没有办法打开这些猪舍,客户们从早上开始一个接一个来购买生猪,他们到达后首先用手中的 ...

  9. 通过PHP current()函数获取未知字符键名数组第一个元素的值

    在开发中经常遇到这样问题,获取数组第一个元素的值,如果是数字索引那还好,直接$array[0],如果键名是字符串,你又未知这个字符串呢?用current()函数就可以做到. 当然,你可以用array_ ...

  10. oracle 博客精选

    http://mp.sohu.com/profile?xpt=b3JhbmV3c0Bzb2h1LmNvbQ==