解决UITableViewCell中的元素无法触发点击事件
来源:互联网 发布:柯维时间矩阵图 编辑:程序博客网 时间:2024/05/17 01:28
需要在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); } } }今天重新看代码,想想能不能找到一个更加简单的方法,因为添加在cell.contentView中的UIButton,有些是可以点击,有些却不可以点击,这让人作势的郁闷啊,所以,我就打印看看cell.contentView.size,居然打印出来的结果是(320, 44),原来保持了默认的大小,所以如果你的button在这个默认的大小范围内,就可以点击,如果超出了这个大小的范围就不能够响应事件了,而我的自定义的UITableViewCell的高度为100,所以需要自己设定cell.contentView的大小,这样就不需要添加复杂的手势操作了。
- (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]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(100, 20, 50, 30); [btn setTitle:@"测试" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; //神奇的设置 cell.contentView.frame = CGRectMake(0, 0, tableView.width, [self tableView:tableView heightForRowAtIndexPath:indexPath]); [cell.contentView addSubview:btn]; } return cell;}- (void)btnClick:(UIButton *)btn { NSLog(@"测试成功");}
阅读全文
0 0
- 解决UITableViewCell中的元素无法触发点击事件
- 按钮无法触发点击事件
- UITableViewCell点击事件无法响应问题
- Storage事件无法触发解决
- Storage事件无法触发解决
- 解决界面失去焦点,点击button按钮,无法触发点击事件
- 解决Android按住空白区域,导致点击其他按钮无法触发点击事件的问题
- jquery 点击 触发上一元素 事件
- Swift UITableViewCell中的按钮点击事件
- 动态生成的元素无法触发事件
- jquery live方法,解决jquery动态添加按钮无法触发点击事件的问题
- 解决weex点击事件触发两次
- unbind方法在事件中的使用,解决同类或同名元素事件触发多次问题
- Jquery实现点击子元素标签只触发子元素点击事件,不触发父元素点击事件
- 解决 UITableViewCell的点击事件和手势的冲突问题
- 解决UIScrollView把uitableviewcell的点击事件屏
- 解决UIScrollView把UITableViewCell的点击事件屏蔽了
- 如何解决UIScrollView把UITableViewCell的点击事件屏蔽
- 随机数的产生
- 日志组件slf4j介绍及配置详解
- 分享一个直接加QQ好友的链接或会话的
- union 与struct的空间计算
- 开机启动bat以及隐藏运行窗口
- 解决UITableViewCell中的元素无法触发点击事件
- 关于caffe LMDB入门 推荐比较好的几个博客
- SylixOS子系统原理--热插拔子系统
- 操作符系列(1)
- c++primer5 3.20答案
- C++11 新特性之Lambda表达式
- URI,URL与URN的区别
- ConstraintLatout怎么用
- sap