回答

4

看起來你想通過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

相關問題