直接上代码:
#import "RootViewController.h"
#import "CollectionViewCell.h"
@interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView ;
@end
@implementation RootViewController
- (void)dealloc {
[_collectionView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
flowLayout.minimumLineSpacing = 5;
flowLayout.minimumInteritemSpacing = 5;
flowLayout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 40) / 4, 120);
flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
flowLayout.headerReferenceSize = CGSizeMake(200, 60);
flowLayout.footerReferenceSize = CGSizeMake(200, 40);
self.collectionView = [[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout] autorelease];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 100;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.titleLable.text = [NSString stringWithFormat:@"(%ld, %ld)", indexPath.section, indexPath.item];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor orangeColor];
return headerView;
}
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
footerView.backgroundColor = [UIColor redColor];
return footerView;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *viewController = [[UIViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@interface CollectionViewCell : UICollectionViewCell
@property (nonatomic, retain) UILabel *titleLable ;
@end
#import "CollectionViewCell.h"
@implementation CollectionViewCell
- (void)dealloc {
[_titleLable release];
[super dealloc];
}
- (UILabel *)titleLable {
if (!_titleLable) {
self.titleLable = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
_titleLable.backgroundColor = [UIColor lightGrayColor];
_titleLable.textColor = [UIColor whiteColor];
_titleLable.font = [UIFont systemFontOfSize:16];
_titleLable.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:_titleLable];
}
return _titleLable;
}
@end