// // MyCollectionViewController.m // CollectionView // // Created by Dannis Mok on 1/2/16. // Copyright (c) 2016 com.uec. All rights reserved. // #import "MyCollectionViewController.h" #import "DetailViewController.h" @interface MyCollectionViewController () @end @implementation MyCollectionViewController{ NSArray *photos; } static NSString * const reuseIdentifier = @"myCell"; - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = NO; // Register cell classes // [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; // Do any additional setup after loading the view. photos = [[NSArray alloc]initWithObjects:@"flower1.jpeg",@"flower2.jpeg",@"flower3.jpg",@"flower4.jpeg",@"flower5.jpeg",nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ #pragma mark - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { #warning Incomplete method implementation -- Return the number of sections return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { #warning Incomplete method implementation -- Return the number of items in the section return [photos count]; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; // Configure the cell UIImageView *imageView = (UIImageView *)[cell viewWithTag:100]; imageView.image = [UIImage imageNamed:[photos objectAtIndex:indexPath.row]]; return cell; } #pragma mark /* // Uncomment this method to specify if the specified item should be highlighted during tracking - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } */ /* // Uncomment this method to specify if the specified item should be selected - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } */ /* // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath { return NO; } - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { return NO; } - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { } */ -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([segue.identifier isEqualToString:@"showDetail"]) { DetailViewController *controller = [segue destinationViewController]; NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems]; NSIndexPath *indexPath = [indexPaths objectAtIndex:0]; NSLog(@"%li",indexPath.row); NSLog(@"%li",indexPath.section); NSLog(@"%@",[photos objectAtIndex:indexPath.row]); controller.photoname = [photos objectAtIndex:indexPath.row]; //controller.photoname = @"flower1.jpeg"; //NSLog(@"%@",controller.photoname); //[self.collectionView deselectItemAtIndexPath:indexPath animated:YES]; } } @end