1

我有一个 UICollectionViewController(带有导航控制器),我想在一个单元格中显示一个图像,该图像“推”到一个普通的 ViewController(每个图像都不同)。我怎么做?

4

1 回答 1

4

似乎您想通过 UICollectionView 构建照片库。如果使用storyBoard,请使用segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"])
    {
        NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];

        // load the image, to prevent it from being cached we use 'initWithContentsOfFile'
        NSString *imageNameToLoad = [NSString stringWithFormat:@"%d_full", selectedIndexPath.row];
        NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"JPG"];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];

        DetailViewController *detailViewController = [segue destinationViewController];
        detailViewController.image = image;
    }
}

如果使用 nib: 在 didSelectItemAtIndexPath 中,使用 self.navigationController 推送。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *imageNameToLoad = [NSString stringWithFormat:@"%d_full", indexPath.row];
    NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"JPG"];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    detailViewController.image = image;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

Apple 的示例代码: https ://developer.apple.com/library/ios/#samplecode/CollectionView-Simple/Introduction/Intro.html

CollecionView 教程:http ://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

于 2013-02-27T06:18:30.880 回答