Ios6 我有一个UICollectionView,我想在一个单元格中显示一个图像,它将转到普通的ViewController。我该怎么做?

Ios6 我有一个UICollectionView,我想在一个单元格中显示一个图像,它将转到普通的ViewController。我该怎么做?,ios6,uinavigationcontroller,xcode4.5,uicollectionview,uicollectionviewcell,Ios6,Uinavigationcontroller,Xcode4.5,Uicollectionview,Uicollectionviewcell,我有一个带有导航控制器的UICollectionViewController,我想在单元格中显示一个图像,该图像“推送”到一个普通的ViewController,每个图像都不同。我该怎么做?您似乎想通过UICollectionView建立照片库。 如果使用故事板,请使用segue - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToS

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

您似乎想通过UICollectionView建立照片库。 如果使用故事板,请使用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;
    }
}
如果在didSelectItemAtIndexPath中使用nib:in,则使用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的示例代码:


CollecionView教程:

似乎您希望通过UICollectionView构建照片库。 如果使用故事板,请使用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;
    }
}
如果在didSelectItemAtIndexPath中使用nib:in,则使用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的示例代码:

CollecionView教程: