我有一个自定义的方法来检测对细胞图像的点击。我还希望找到图像的相关单元格的索引路径,并在函数中使用它。下面是我正在使用的:
CellforRowAtIndexPath:
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellImageTapped:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];方法im尝试在以下位置获取索引路径:
-(void)cellImageTapped:(id)sender {
if(videoArray.count > 0){
Video *currentVideo = [videoArray objectAtIndex:INDEX_PATH_OF_CELL_IMAGE];
//do some stuff
}
}我不知道如何传递索引路径。有什么想法吗?
发布于 2013-10-18 11:21:10
简单的方法:
点处单元格的索引路径
代码是:
-(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
}
}发布于 2013-10-18 10:55:53
我建议用这种方式来获取具有自定义子视图的单元的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);
}发布于 2013-10-18 08:24:30
向UITableViewDataSource的tableView:cellForRowAtIndexPath:方法中的UIImageView添加一个标记。
cell.imageView.tag = indexPath.row;https://stackoverflow.com/questions/19439434
复制相似问题