1

我正在做一些涉及其中包含照片的集合视图,并且在选择其中一个单元格时,它将进入一个显示更大图像的新视图。

继承人为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);
}

我的视图总是空的,所以我添加了打印行语句,看起来输出总是像

Selected cell at <NSIndexPath 0x1e084940> 2 indexes [0, 0], detail view controller 

所以我的问题是,为什么 NSIndexPath 总是一对 2 索引,以及如何在我的 prepareforsegue 中使用它来设置 segue 的视图

谢谢

4

3 回答 3

1

prepareForSegue:sender:中,您期望sender成为UICollectionViewCell.

collectionView:didSelectItemAtIndexPath:中,您将self(your 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-15T07:52:50.770 回答
0

NSIndexPath是用于表示目录树的文件结构。在表 (indexPathForCell:) 的上下文中,它包含一个节和行索引。

索引路径中的每个索引表示从树中的一个节点到另一个更深节点的子数组的索引。例如,索引路径 1.4.3.2 指定图 1 所示的路径。

文档中的更多信息

于 2012-12-15T07:52:19.950 回答
0

NSIndexPath 主要用于 UITableView 它将考虑 Section 以及 Row 的地方..

所以无论你想在哪里使用 NSIndexPath,你都可以访问行和部分。

所以我要求你使用 indexpath.row 作为你的索引变量,如果 indexpath.section 对你没有用..

于 2012-12-15T12:52:38.697 回答