有时候自定义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. 制作servlet模板

    制作servlet模板 选中window-->preference--->搜索template--->选中java下面的template new一个 Name的设置,当你在eclip ...

  2. validate插件实现表单效验(二)

    一款优秀的表单验证插件——validation插件 特点: l  内置验证规则:拥有必填.数字.email.url和信用卡号码等19类内置验证规则 l  自定义验证规则:可以很方便的自定义验证规则 l ...

  3. HDU 2639 Bone Collector II【01背包 + 第K大价值】

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  4. (寒假集训)洛谷 P2058 海港

    小K是一个海港的海关工作人员,每天都有许多船只到达海港,船上通常有很多来自不同国家的乘客. 小K对这些到达海港的船只非常感兴趣,他按照时间记录下了到达海港的每一艘船只情况:对于第i艘到达的船,他记录了 ...

  5. sqlite db-journal文件产生原因及说明

    今天在Android中将sqlite的数据库文件生成在SD卡上的过程中,发现生成的.db文件的旁边 生成了一个大小为0的与数据库文件同名的.db-journal文件,不明白此文件的用途,于是 goog ...

  6. Android kernel Crash后,定位出错点的方法

    转载:http://blog.csdn.net/wlwl0071986/article/details/11635749 1. 将/prebuild/gcc/linux-x86/arm/arm-lin ...

  7. 异常如果一直被throws抛出的话就会被jvm异常处理器处理了

    异常如果一直被throws抛出的话就会被jvm异常处理器处理了,这时jvm会跳出正常运行状态. 异常如果一直被throws抛出的话就会被jvm异常处理器处理了,这时jvm会跳出正常运行状态. 异常如果 ...

  8. JAVA常见算法题(七)

    package com.xiaowu.demo; /** * 输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. * * @author WQ * */ public class De ...

  9. THINKPHP nginx设置路由为PATHINFO模式

    首先THINKPHP配置文件中设置 //url访问模式为rewrite模式 'URL_MODEL'=>'2', 然后再在nginx.conf文件中,找到这一条语句 #access_log log ...

  10. DEDECMS首页和列表页调用单个图片集里的多张图片

    /**DEDECMS首页和列表页调用单个图片集里的多张图片 function Getimg($aid,$imgwith=110,$imgheight=110,$num=0) { global $dsq ...