我正在做一些涉及其中包含照片的集合视图的内容,并且在选择其中一个单元格后,它将进入一个新视图,在该视图中显示更大的图像。
继续为segue做准备
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"showPhotoSegue"]) {
NSIndexPath *ip = [self.photoCollectionView indexPathForCell:sender];
PhotoDisplayViewController *viewController = segue.destinationViewController;
Photo* photo = [self.fetchedResultsController objectAtIndexPath:ip];
NSLog(@"setting PHOTO at indexPath %@", ip);
[viewController setPhoto:[UIImage imageWithContentsOfFile:photo.url]];
}
}
并且heres didSelectItemAtIndexPath
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = @"showPhotoSegue";
[self performSegueWithIdentifier:identifier sender:self];
NSLog(@"Selected item at %@", indexPath);
}
我的视图总是空的,所以我添加了打印行语句,似乎输出总是像
Selected cell at <NSIndexPath 0x1e084940> 2 indexes [0, 0], detail view controller
所以我的问题是,为什么NSIndexPath总是一对2个指标,以及我如何在我的prepareforsegue中使用它来设置segue的视图
由于
答案 0 :(得分:1)
在prepareForSegue:sender:
中,您希望sender
为UICollectionViewCell
。
在collectionView:didSelectItemAtIndexPath:
中,您将self
(您的UICollectionViewDelegate
)作为sender
参数传递。我怀疑你的集合视图委托是而不是集合视图单元格。
不是将集合视图委托作为sender
发送,而是希望以sender
的形式接收单元格,为什么不将索引路径作为sender
传递,并希望以{{sender
的形式接收它1}}?
static NSString const *kShowPhotoSegueIdentifier = @"showPhotoSegue";
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:kShowPhotoSegueIdentifier sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:kShowPhotoSegueIdentifier]) {
[self prepareForShowPhotoSegue:segue withIndexPath:sender];
}
}
- (void)prepareForShowPhotoSegue:(UIStoryboardSegue *)segue withIndexPath:(NSIndexPath *)indexPath {
PhotoDisplayViewController *viewController = segue.destinationViewController;
Photo* photo = [self.fetchedResultsController objectAtIndexPath:indexPath];
[viewController setPhoto:[UIImage imageWithContentsOfFile:photo.url]];
}
答案 1 :(得分:0)
NSIndexPath
是一个用于表示目录树的文件结构。在表(indexPathForCell :)的上下文中,它包含一个部分&amp;行索引。
索引路径中的每个索引都表示数组的索引 孩子从树中的一个节点到另一个节点,更深,节点。对于 例如,索引路径1.4.3.2指定了图1中所示的路径。
答案 2 :(得分:0)
NSIndexPath主要用于UITableView它将考虑部分以及行的那个..
所以,无论您想使用NSIndexPath,您都可以访问 行和部分..
所以我请求你使用indexpath.row作为索引变量, 如果indexpath.section对你没用..