首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tableViewCell的indexPath

tableViewCell的indexPath
EN

Stack Overflow用户
提问于 2013-10-18 08:10:18
回答 5查看 4.3K关注 0票数 2

我有一个自定义的方法来检测对细胞图像的点击。我还希望找到图像的相关单元格的索引路径,并在函数中使用它。下面是我正在使用的:

CellforRowAtIndexPath:

代码语言:javascript
复制
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellImageTapped:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];

方法im尝试在以下位置获取索引路径:

代码语言:javascript
复制
  -(void)cellImageTapped:(id)sender {
   if(videoArray.count > 0){
       Video *currentVideo = [videoArray objectAtIndex:INDEX_PATH_OF_CELL_IMAGE];
     //do some stuff          
  }
}

我不知道如何传递索引路径。有什么想法吗?

EN

回答 5

Stack Overflow用户

发布于 2013-10-18 11:21:10

简单的方法:

  • 获取接触点
  • ,然后获取

点处单元格的索引路径

代码是:

代码语言:javascript
复制
-(void)cellImageTapped:(id)sender {
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;
    CGPoint point = [tap locationInView:theTableView];

    NSIndexPath *theIndexPath = [theTableView indexPathForRowAtPoint:point];

    if(videoArray.count > 0){
        Video *currentVideo = [videoArray objectAtIndex:theIndexPath];
        //do some stuff
    }
}
票数 5
EN

Stack Overflow用户

发布于 2013-10-18 10:55:53

我建议用这种方式来获取具有自定义子视图的单元的indexPath -(兼容iOS 7以及所有以前版本的)

代码语言:javascript
复制
- (void)cellImageTapped:(UIGestureRecognizer *)gestureRecognizer
{
    UIView *parentCell = gestureRecognizer.view.superview;

    while (![parentCell isKindOfClass:[UITableViewCell class]]) {   // iOS 7 onwards the table cell hierachy has changed.
        parentCell = parentCell.superview;
    }

    UIView *parentView = parentCell.superview;

    while (![parentView isKindOfClass:[UITableView class]]) {   // iOS 7 onwards the table cell hierachy has changed.
        parentView = parentView.superview;
    }


    UITableView *tableView = (UITableView *)parentView;
    NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)parentCell];

    NSLog(@"indexPath = %@", indexPath);
}
票数 4
EN

Stack Overflow用户

发布于 2013-10-18 08:24:30

UITableViewDataSourcetableView:cellForRowAtIndexPath:方法中的UIImageView添加一个标记。

代码语言:javascript
复制
cell.imageView.tag = indexPath.row;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19439434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档