tableViewCell的indexPath

Jos*_*osa 2 cocoa-touch objective-c uitableview ios

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

的cellForRowAtIndexPath:

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

方法我试图获取索引路径:

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

我不知道如何传递索引路径.有任何想法吗?

vie*_*one 6

简单方法:

  • 获得触摸点

  • 然后获取单元格的索引路径

代码是:

-(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
    }
}
Run Code Online (Sandbox Code Playgroud)