我有一个UICollectionViewController(带有导航控制器),我想在一个Cell中显示一个图像,“推”到普通的ViewController(每个图像都不同)。我该怎么做?
答案 0 :(得分: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 push。
-(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