Ios tableViewCell的索引XPath
我有一个自定义方法来检测细胞图像上的点击。我还想找到图像相关单元的索引路径,并在函数中使用它。以下是我正在使用的: CellforRowAtIndexPath: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
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
}
}