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 = ; 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添加手势没有响应的解决方法的更多相关文章
- JabRef中添加中文文献出现乱码 解决方法
JabRef中添加中文文献出现乱码 解决方法 问题描述 JaBRef是一款开源的文献管理软件,主要用来管理bibtex格式的参考文献,可以与LATEX配合使用,方便论文参考文献的使用.文献管理 ...
- 在MonoGame中SetRenderTarget会把后备缓冲区清除的解决方法
在MonoGame中SetRenderTarget会把后备缓冲区清除的解决方法: 在构造函数中添加事件:graphics.PreparingDeviceSettings += Graphics_Pre ...
- 检索 COM 类工厂中 CLSID 为 {10020200-E260-11CF-AE68-00AA004A34D5} 的组件时失败,解决方法如下:
检索 COM 类工厂中 CLSID 为 {10020200-E260-11CF-AE68-00AA004A34D5} 的组件时失败,解决方法如下: 第 一步:首先将msvcr71.dll, SQLD ...
- log4j中Spring控制台输出Debug级信息过多解决方法
log4j中Spring控制台输出Debug级信息过多解决方法 >>>>>>>>>>>>>>>>> ...
- universal image loader在listview/gridview中滚动时重复加载图片的问题及解决方法
在listview/gridview中使用UIL来display每个item的图片,当图片数量较多需要滑动滚动时会出现卡顿,而且加载过的图片再次上翻后依然会重复加载(显示设置好的加载中图片) 最近在使 ...
- MySQL中遇到的几种报错及其解决方法
MySQL中遇到的几种报错及其解决方法 1.[Err] 1064 - You have an error in your SQL syntax; check the manual that corre ...
- [datatable]关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法
-- :09关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法 在实际编程工程中,常常遇到这样的情况:DataTable并不 ...
- Eclipse中SVN修改的*星号没了,解决方法
Eclipse中SVN修改的*星号没了,解决方法 打开Preference 第一步:去掉外加的 ">" 第二步:勾选Outgoing changes 这样做之后," ...
- Python3中使用HTMLTestRunner报No module named 'StringIO'解决方法
今天在学习使用HTMLTestRunner生成测试报告时遇到一个报错,如图所示: 网上搜索了下“No module named 'StringIO'”解决方法,原来我用的是Python 3.X版本,而 ...
随机推荐
- 使用composer出现 Cannot find module (SNMPv2-TC) 等错误的解决方法
Cannot find module (SNMPv2-TC): At line 10 in /usr/share/snmp/mibs/UCD-DLMOD-MIB.txt Cannot find mod ...
- 给object数组进行排序(排序条件是每个元素对象的属性个数)
从汤姆大叔的博客里看到了6个基础题目:本篇是第3题 - 给object数组进行排序(排序条件是每个元素对象的属性个数) 解题关键: 1.Array.sort的用法 2.object的属性数量的统计 解 ...
- 深入分析CVE-2016-5195 Dirty Cow
前面一段时间,这个编号为CVE-2016-5195的漏洞刷爆了各个安全相关的博客和网站,这个漏洞可以对任意可读文件进行写操作从而导致提权,通杀了包括Android在内的绝大多数linux版本,,影响不 ...
- Jboss ESB简介及开发实例
一.Jboss ESB的简介 1. 什么是ESB. ESB的全称是Enterprise Service Bus,即企业服务总线.ESB是过去消息中间件的发展,ESB采用了“总线”这样一 ...
- 六. 异常处理7.throw:异常的抛出
到目前为止,你只是获取了被Java运行时系统抛出的异常.然而,程序可以用throw语句抛出明确的异常.Throw语句的通常形式如下: throw ThrowableInstance;这里,Thr ...
- Mysql insert without auto-increase when duplicate
INSERT INTO video_tag_all(tagname,ctime) FROM video_tag_all WHERE (SELECT last_insert_id(id) FROM vi ...
- Flex this
为了便于对比和叙述,我们先上一段最简单的js+html代码:<input type="button" value="test" id="htmB ...
- 三种Web前端框架比较与介绍--Vue, react, angular
一.Angular 1.MVVM(Model)(View)(View-model): 2.模块化(Module)控制器(Contoller)依赖注入: 3.双向数据绑定:界面的操作能实时反映到数据,数 ...
- 【ASP.NET】 中 system.math 函数使用
1 /* 字段 */ 2 Math.E; //2.71828182845905 3 Math.PI; //3.14159265358979 4 5 /* 静态方法 */ 6 Math.Abs; //绝 ...
- git reset,git checkout区别
https://www.cnblogs.com/houpeiyong/p/5890748.html git reset HEAD~1 将HEAD指向倒数第二次提交,最后一次提交删除,文件还在 gi ...