Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.m
- // ExploreUICollectionView
- //
- // Created by Michael Lehman on 1/6/13.
- // Copyright (c) 2013 Developer Extraordinaire. All rights reserved.
- //
- #import "ViewController.h"
- @interface ViewController ()
- @property (strong, nonatomic) IBOutlet UICollectionView *myCollectionView;
- @property (nonatomic, strong) NSMutableArray *cellData;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.myCollectionView.delegate = self;
- self.myCollectionView.dataSource = self;
- self.cellData = [[NSMutableArray alloc]init];
- for(int i=0; i < 100; i++)
- {
- [self.cellData addObject:[NSString stringWithFormat:@"#%d", i+1]];
- }
- UINib *cellNib = [UINib nibWithNibName:@"CVCell"
- bundle:nil];
- [self.myCollectionView registerNib:cellNib
- forCellWithReuseIdentifier:@"CVCell"];
- UICollectionViewFlowLayout *layout =
- [[UICollectionViewFlowLayout alloc] init];
- [layout setItemSize:CGSizeMake(100, 100)];
- [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
- [self.myCollectionView setCollectionViewLayout:layout];
- }
- -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *cellIdentifier = @"CVCell";
- UICollectionViewCell *cell =
- [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier
- forIndexPath:indexPath];
- UILabel *cellTitle = (UILabel *)[cell viewWithTag:1];
- cellTitle.text = [self.cellData objectAtIndex:indexPath.row];
- return cell;
- }
- -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
- {
- return 1;
- }
- -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- return [self.cellData count];
- }
- -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
- UILabel *cellTitle = (UILabel *)[cell viewWithTag:1];
- NSLog(@"You selected cell: %@", cellTitle.text);
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement