UITableViewCell中自定义cell上UILabel添加手势没有响应解决方案
来源:互联网 发布:c4d for mac下载 编辑:程序博客网 时间:2024/06/04 18:59
- 情况一:自定义UITableViewCell,且cell中添加了一个UILabel,我们的目的是给该label添加一个手势。
- 情况二:原生cell上添加了一个UIView,我们的目的是给UIView上的lable添加一个手势。
但是以上两种情况如果按照常规的添加方法,发现所添加的手势并不能响应。以下为解决方法:将手势添加到UITableView上或者添加到UIView上(即使添加到lable的父视图上),两种情况的解决方案类似,以下附上第一种情况的代码:(代码有点乱,还望海涵)
@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]; //或者给 UIview添加手势(前提你要创建)}#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 - 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) { //for ( UIView *view in _perCardView.subviews)->自定义view if ([view isKindOfClass:[UILabel class]]) { label = (UILabel *)view; //lable = (UILel *)[自定义view viewWithTag:2]; break; } } //获得当前手势点击在UILabe中的坐标 CGPoint p = [gesture locationInView:label]; //CGPoint p = [gesture locationInView:自定义view]; //看看手势点的坐标是不是在UILabel中 if (CGRectContainsPoint(label.frame, p)) { NSLog(@"label text : %@", label.text); } //--------------------------------------------- }}
注:如有错误,还望指正!
0 0
- UITableViewCell中自定义cell上UILabel添加手势没有响应解决方案
- UITableViewCell中自定义cell添加手势没有响应解决方案
- 给UILabel添加自定义手势后没有反应
- 自定义cell,添加uilable,并且给lable添加tap手势,不响应的问题,对应解决方法
- UIWebView中添加手势不响应的解决方案
- ios中自定义cell,自定义UITableViewCell
- uitableviewcell添加长按手势 并获取cell
- iOS 系统自带UItableviewcell上添加其它控件 不需要自定义cell 简单粗暴
- 自定义cell中UILabel文字换行显示
- 为 自定义 Cell 上的控件(Label、ImageView)添加手势
- 代码自定义cell 并添加button响应
- 在UITableViewCell上添加多行的UILabel
- 在UITableView或UICollectionView的自定义的cell里添加手势或button,响应方法时如何传参数
- 自定义UITableViewCell重用cell
- UITableViewCell 自定义手势动作
- iOS 自定义UITableViewCell上添加UIButton按钮实现选中的是哪个按钮对应的cell,用代理实现。
- UITableViewCell中去除没有内容的cell的分割线
- Cell上的subView添加手势 与 cell点击手势冲突
- PHP编译动态文件
- HDOJ 3785 寻找大富翁(优先队列,水)
- hdu 5326 Work
- openGL构造二维旋转矩阵
- IP Address(POJ--2105
- UITableViewCell中自定义cell上UILabel添加手势没有响应解决方案
- Java 反射
- 【HTML学习】第五章 富媒体
- 关于static在java和c++中的用法小谈(二)
- Xcode的git的ignore配置:(稍候持续更新...)
- HashMap的实现原理
- 解决Nginx反向代理后无法加载静态文件的问题
- Android Studio导入项目
- 懒省事的小明