1
我有一個UICollectionViewController(帶有一個導航控制器),我想在一個單元中顯示一個圖像,這個單元'推送'到一個普通的ViewController(每個圖像都不相同)。我怎麼做?我有一個UICollectionView,我想在一個單元格中顯示一個圖像,該圖像轉到一個普通的ViewController。我怎麼做?
我有一個UICollectionViewController(帶有一個導航控制器),我想在一個單元中顯示一個圖像,這個單元'推送'到一個普通的ViewController(每個圖像都不相同)。我怎麼做?我有一個UICollectionView,我想在一個單元格中顯示一個圖像,該圖像轉到一個普通的ViewController。我怎麼做?
看起來你想通過UICollectionView構建照片庫。 如果用故事板,使用賽格瑞
- (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,使用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];
}
示例代碼: 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