我正在做的事情涉及到一个包含照片的集合视图,当选择其中一个单元格时,它将分割成一个新视图,其中显示一个较大的图像。
在这里为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]];
}
}这里是didSelectItemAtIndexPath
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = @"showPhotoSegue";
[self performSegueWithIdentifier:identifier sender:self];
NSLog(@"Selected item at %@", indexPath);
}我的视图总是空的,所以我添加了print line语句,似乎输出总是这样的
Selected cell at <NSIndexPath 0x1e084940> 2 indexes [0, 0], detail view controller 因此,我的问题是,为什么NSIndexPath总是一对2索引,以及如何在prepareforsegue中使用它来设置段的视图
谢谢
发布于 2012-12-15 15:52:51
在prepareForSegue:sender:中,您希望sender是一个UICollectionViewCell。
在collectionView:didSelectItemAtIndexPath:中,您将self (您的UICollectionViewDelegate)作为sender参数传递。我怀疑您的集合视图委托不是集合视图单元格。
与其将集合视图委托作为sender发送并期望以sender形式接收单元格,为什么不将索引路径作为sender传递并期望以sender形式接收它呢
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]];
}发布于 2012-12-15 15:52:20
NSIndexPath是用于表示目录树的文件结构。在表(indexPathForCell:)的上下文中,它包含一个段和行索引。
索引路径中的每个索引表示从树中的一个节点到另一个更深的节点的子节点数组中的索引。例如,索引路径1.4.3.2指定了图1所示的路径。
发布于 2012-12-15 20:52:39
NSIndexPath主要用于UITableView,它将考虑节和行。
因此,无论您想在何处使用NSIndexPath,您都可以访问相应的行和节。
因此,如果indexpath.section对您没有用处,我请求您使用indexpath.row作为索引变量。
https://stackoverflow.com/questions/13890373
复制相似问题