CollectionView/ViewController.m
/* |
Copyright (C) 2015 Apple Inc. All Rights Reserved. |
See LICENSE.txt for this sample’s licensing information |
Abstract: |
The primary view controller for this app. |
*/ |
#import "ViewController.h" |
#import "DetailViewController.h" |
#import "Cell.h" |
NSString *kDetailedViewControllerID = @"DetailView"; // view controller storyboard id |
NSString *kCellID = @"cellID"; // UICollectionViewCell storyboard id |
@implementation ViewController |
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section; |
{ |
return 32; |
} |
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; |
{ |
// we're going to use a custom UICollectionViewCell, which will hold an image and its label |
// |
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath]; |
// make the cell's title the actual NSIndexPath value |
cell.label.text = [NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section]; |
// load the image for this cell |
NSString *imageToLoad = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; |
cell.image.image = [UIImage imageNamed:imageToLoad]; |
return cell; |
} |
// the user tapped a collection item, load and set the image on the detail view controller |
// |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender |
{ |
if ([segue.identifier isEqualToString:@"showDetail"]) |
{ |
NSIndexPath *selectedIndexPath = [self.collectionView indexPathsForSelectedItems][0]; |
// load the image, to prevent it from being cached we use 'initWithContentsOfFile' |
NSString *imageNameToLoad = [NSString stringWithFormat:@"%ld_full", (long)selectedIndexPath.row]; |
UIImage *image = [UIImage imageNamed:imageNameToLoad]; |
DetailViewController *detailViewController = segue.destinationViewController; |
detailViewController.image = image; |
} |
} |
@end |
Copyright © 2015 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2015-10-22