首先自定義段頭類
@interface MyFooterView : UICollectionReusableView
在.m中實(shí)現(xiàn)初始化,段尾同理
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
_titleLab = [[UILabel alloc]init];
_titleLab.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-10);
_titleLab.textAlignment = NSTextAlignmentCenter;
_titleLab.backgroundColor = [UIColor yellowColor];
[self addSubview:_titleLab];
}
return self;
}
創(chuàng)建collectionview注冊cell的同時(shí)注冊段頭,段尾同理
[self.NewgoodsCV registerClass:[MyHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
在代理方法中設(shè)置?段頭段尾
//設(shè)置頭尾部內(nèi)容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView = nil;
if (kind == UICollectionElementKindSectionHeader) {
//定制頭部視圖的內(nèi)容
MyHeaderView *headerV = (MyHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
headerV.titleLab.text = @"段頭";
reusableView = headerV;
}
//定制尾部視圖的內(nèi)容
if (kind == UICollectionElementKindSectionFooter){
MyFooterView *footerV = (MyFooterView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
footerV.titleLab.text = @"段尾";
reusableView = footerV;
}
return reusableView;
}
效果圖: