#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;
}