首先自定义段头类
@interface MyFooterView : UICollectionReusableView
在.m中实现初始化,段尾同理
- (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;
}
创建collectionview注册cell的同时注册段头,段尾同理
[self.NewgoodsCV registerClass:[MyHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
在代理方法中设置�段头段尾
//设置头尾部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView = nil;
if (kind == UICollectionElementKindSectionHeader) {
//定制头部视图的内容
MyHeaderView *headerV = (MyHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
headerV.titleLab.text = @"段头";
reusableView = headerV;
}
//定制尾部视图的内容
if (kind == UICollectionElementKindSectionFooter){
MyFooterView *footerV = (MyFooterView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
footerV.titleLab.text = @"段尾";
reusableView = footerV;
}
return reusableView;
}
效果图: