标签:
#import "FirstHeaderReusableView.h"
#import "FirstFooterReusableView.h"
#define ITEMWIDTH ([UIScreen mainScreen].bounds.size.width - 30) / 2
#define ITEMHEIGHT 180
@interface OneViewController ()
@property (nonatomic, strong)FirstHeaderReusableView *header;
@end
@implementation OneViewController
static NSString * const reuseIdentifier = @"cell";
//懒加载创建数组
- (NSMutableArray *)dataSource{
if (!_dataSource) {
self.dataSource = [NSMutableArray arrayWithCapacity:0];
}
return _dataSource;
}
- (NSMutableArray *)rollpic{
if (!_rollpic) {
self.rollpic = [NSMutableArray arrayWithCapacity:0];
}
return _rollpic;
}
- (instancetype)init{
//创建网格化布局方式
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//设置布局属性
flowLayout.itemSize = CGSizeMake(ITEMWIDTH, ITEMHEIGHT);
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//给header和footer腾个地儿~
flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 200);
flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 200);
self = [self initWithCollectionViewLayout:flowLayout];
if (self) {
// insert code here...
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionView.backgroundColor = [UIColor whiteColor];
//注册对应的cell
// [self.collectionView registerClass:[FirstCustomViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.collectionView registerNib:[UINib nibWithNibName:@"HomePageViewCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
//注册页眉或者页脚
[self.collectionView registerClass:[FirstHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
[self.collectionView registerClass:[FirstFooterReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"footer"];
}
//返回每个分区的页眉或者页脚
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
//返回页眉
self.header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
reusableview = self.header;
}
if (kind == UICollectionElementKindSectionFooter){
FirstFooterReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
reusableview = footerview;
}
return reusableview;
}
UICollectionViewController既有headerView又有footerView。
标签:
原文地址:http://www.cnblogs.com/aixixi/p/5156527.html