- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout = flowLayout;
_flowLayout.minimumLineSpacing = 3;
_flowLayout.minimumInteritemSpacing = 3;
_flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 35);
_flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 10);
self = [super initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
if (self) {
self.showsVerticalScrollIndicator = NO;
self.bounces = NO;
self.alwaysBounceVertical = NO;
self.scrollsToTop = YES;
// self.pagingEnabled = NO;
self.dataSource = self;
self.delegate = self;
//self.backgroundColor = [UIColor clearColor];
self.backgroundColor = OtherLineColor;
//注册cell单元格
[self registerClass:[CategoryCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
//下面的注册组的头尾部view,这个必须实现,否则会崩
//注册header单元格
[self registerClass:[CategoryCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ID];
//注册footer单元格
[self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footID];
}
return self;
}
//组的头尾部的代码实现
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *reusableView = nil;
//组的头部
if (kind == UICollectionElementKindSectionHeader ) {
//CategoryCollectionReusableView是自定义的继承UICollectionReusableView的子类;
CategoryCollectionReusableView *categoryCollectionReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:ID forIndexPath:indexPath];
if (indexPath.section == 0) {
categoryCollectionReusableView.title = @"全部分类";
}else if (indexPath.section == 1){
categoryCollectionReusableView.title = @"热门品牌";
}
reusableView = categoryCollectionReusableView;
}
//组的尾部
if (kind == UICollectionElementKindSectionFooter) {
UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footID forIndexPath:indexPath];
reusableView = footer;
}
return reusableView;
}