Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios tableViewCell的索引XPath_Ios_Objective C_Cocoa Touch_Uitableview - Fatal编程技术网

Ios tableViewCell的索引XPath

Ios tableViewCell的索引XPath,ios,objective-c,cocoa-touch,uitableview,Ios,Objective C,Cocoa Touch,Uitableview,我有一个自定义方法来检测细胞图像上的点击。我还想找到图像相关单元的索引路径,并在函数中使用它。以下是我正在使用的: CellforRowAtIndexPath: UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellImageTapped:)]; tapped.numberOfTapsRequired = 1; [cell.imageVie

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

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

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

UITableViewDataSource
tableView:cellforrowatinexpath:
方法的
UIImageView
中添加一个标记

cell.imageView.tag = indexPath.row;

使用委托方法didSelectRowAtIndexPath:方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self cellImageTapped:indexPath];
}
然后您可以将索引传递给函数,即

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

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

-(void)cellImageTapped:(id)sender {

UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;

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

}

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

简单方法:

  • 抓住要点

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

代码是:

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

点击单元格时未调用DidSelectRowatineXpath。否则这将是完美的:(实际上我是,我只是没有列出它,但我无权访问函数中的单元格。您可以访问imageView。使用((UITapgestureRecognitizer*)发送方)cellImageTapped:方法中的.view.tag。点击手势识别器的视图是单击的imageView。这是假设TableView中只有一个部分这是最佳答案。
-(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
    }
}