我有一个UICollectionView,我想在Cell中显示一个图像,这是一个普通的ViewController。我怎么做? [英] I have a UICollectionView and i want to show an image in a Cell, that goes to a normal ViewController. How do i do that?
问题描述
我有一个UICollectionViewController(带有导航控制器),我想在一个Cell中显示一个图像,推到普通的ViewController(每个图像都不同)。我该怎么做?
I have a UICollectionViewController (with a navigation controller) and i want to show an image in a Cell that 'pushes' to a normal ViewController (different by every image). How do i do that?
推荐答案
似乎你想通过UICollectionView建立照片库。
如果使用storyBoard,请使用segue
Seem you want to build photo gallery by UICollectionView. If use storyBoard, use 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:inside didSelectItemAtIndexPath,请使用self.navigationController推送。
If use nib: inside didSelectItemAtIndexPath, use 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-简单/简介/ Intro.html
Sample code from 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
CollecionView tutorial: http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
这篇关于我有一个UICollectionView,我想在Cell中显示一个图像,这是一个普通的ViewController。我怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!