【问题标题】:indexPath of tableViewCell [duplicate]tableViewCell 的 indexPath [重复]
【发布时间】:2013-10-18 00:10:18
【问题描述】:

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

CellforRowAtIndexPath:

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellImageTapped:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];

我试图在以下位置获取索引路径的方法:

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

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

【问题讨论】:

    标签: ios objective-c cocoa-touch uitableview


    【解决方案1】:

    简单的方法:

    • 获取接触点

    • 然后得到该点单元格的索引路径

    代码是:

    -(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
        }
    }
    

    【讨论】:

    • 这是最好的答案。
    【解决方案2】:

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

    - (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);
    }
    

    【讨论】:

      【解决方案3】:

      在UITableViewDataSource 的tableView:cellForRowAtIndexPath: 方法中为UIImageView 添加标签。

      cell.imageView.tag = indexPath.row;
      

      【讨论】:

      • 我实际上是,我只是没有列出它,但我无法访问我的函数中的单元格。
      • 您可以访问 imageView。在你的 cellImageTapped: 方法中使用 ((UITapGestureRecognizer *) sender).view.tag。点击手势识别器的视图是被点击的 imageView。
      • 这是假设tableView中只有一个section
      【解决方案4】:

      我最终使用了发送者视图的标签。希望这会对某人有所帮助,因为我浪费了一个小时来寻找答案。

      -(void)cellImageTapped:(id)sender {
      
      UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
      
                  if(videoArray.count > 0){
                      NSInteger datIndex = gesture.view.tag;
                      Video *currentVideo = [videoArray objectAtIndex:datIndex];
                  }
      
      }
      

      【讨论】:

        【解决方案5】:

        使用委托方法 didSelectRowAtIndexPath: 方法

        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
            [self cellImageTapped:indexPath];
        }
        

        然后您可以将索引传递给函数,即

        -(void)cellImageTapped:(NSIndexPath *)indexPath
        {
            Video *currentVideo = [videoArray objectAtIndex:indexPath.row];
        }
        

        【讨论】:

        • didSelectRowAtIndexPath 在我点击单元格时不会被调用。否则这将是完美的:(
        猜你喜欢
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 2019-02-26
        • 2016-08-11
        • 2019-09-17
        • 2020-10-01
        • 2019-01-12
        • 1970-01-01
        相关资源
        最近更新 更多