Advertisement
afarber

ExploreUICollectionView/ViewController.m

Dec 10th, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.m
  3. //  ExploreUICollectionView
  4. //
  5. //  Created by Michael Lehman on 1/6/13.
  6. //  Copyright (c) 2013 Developer Extraordinaire. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()
  12. @property (strong, nonatomic) IBOutlet UICollectionView *myCollectionView;
  13. @property (nonatomic, strong) NSMutableArray *cellData;
  14.  
  15. @end
  16.  
  17. @implementation ViewController
  18.  
  19. - (void)viewDidLoad
  20. {
  21.     [super viewDidLoad];
  22.     self.myCollectionView.delegate = self;
  23.     self.myCollectionView.dataSource = self;
  24.    
  25.     self.cellData = [[NSMutableArray alloc]init];
  26.    
  27.     for(int i=0; i < 100; i++)
  28.     {
  29.         [self.cellData addObject:[NSString stringWithFormat:@"#%d", i+1]];
  30.     }
  31.    
  32.     UINib *cellNib = [UINib nibWithNibName:@"CVCell"
  33.                                     bundle:nil];
  34.     [self.myCollectionView registerNib:cellNib
  35.             forCellWithReuseIdentifier:@"CVCell"];
  36.    
  37.     UICollectionViewFlowLayout *layout =
  38.     [[UICollectionViewFlowLayout alloc] init];
  39.    
  40.     [layout setItemSize:CGSizeMake(100, 100)];
  41.     [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
  42.    
  43.     [self.myCollectionView setCollectionViewLayout:layout];
  44. }
  45.  
  46. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  47. {
  48.     static NSString *cellIdentifier = @"CVCell";
  49.     UICollectionViewCell *cell =
  50.     [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier
  51.                                               forIndexPath:indexPath];
  52.    
  53.     UILabel *cellTitle = (UILabel *)[cell viewWithTag:1];
  54.    
  55.     cellTitle.text = [self.cellData objectAtIndex:indexPath.row];
  56.     return cell;
  57. }
  58.  
  59. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  60. {
  61.     return 1;
  62. }
  63.  
  64. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  65. {
  66.     return [self.cellData count];
  67. }
  68.  
  69. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  70. {
  71.     UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  72.     UILabel *cellTitle = (UILabel *)[cell viewWithTag:1];
  73.     NSLog(@"You selected cell: %@", cellTitle.text);
  74. }
  75.  
  76. - (void)didReceiveMemoryWarning
  77. {
  78.     [super didReceiveMemoryWarning];
  79.     // Dispose of any resources that can be recreated.
  80. }
  81.  
  82. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement