CollectionView Header And Footer 的使用
Posted 我叫小小虎
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CollectionView Header And Footer 的使用相关的知识,希望对你有一定的参考价值。
CollectionView Header And footer 的添加和设置
建议先看CollectionView 第一篇:http://www.jianshu.com/p/a1614404ae96
注册Header 和 Footer
//注册区头
[_myCollectionView registerClass:[HeaderCRView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
//注册区尾
[_myCollectionView registerClass:[HeaderCRView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
自定义Header子类 ---->HeaderCRView (创建)
自定义的话,header子类要继承自 UICollectionReusableView。
#pragma mark 自定义区头 区尾 样式
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"headerView";
NSString *cellfooter = @"footerView";
HeaderCRView *cell= nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
cell = (HeaderCRView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CellIdentifier forIndexPath:indexPath];
[cell initCollectionHeaderViewindex:indexPath.section];
}
if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
cell = (HeaderCRView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:cellfooter forIndexPath:indexPath];
[cell initCollectionFootViewindex:indexPath.section];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//设置区头高度
if (section == 0) {
CGSize size = CGSizeMake(kUIScreenWidth, 135+35);
return size;
}
CGSize size = CGSizeMake(kUIScreenWidth, 35);
return size;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
//设置区尾 高度
CGSize size = CGSizeMake(kUIScreenWidth, 10);
return size;
}
HeaderCRView .h
@interface HeaderCRView : UICollectionReusableView
//区头
- (void)initCollectionHeaderViewindex:(NSInteger *)index;
//区尾
- (void)initCollectionFootViewindex:(NSInteger *)indexPath;
@end
HeaderCRView .m
- (void)initCollectionHeaderViewindex:(NSInteger *)index {
if (index == 0) {
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 135, kUIScreenWidth, 35)];
headerView.backgroundColor = [UIColor whiteColor];
[self addSubview:headerView];
UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 80, 15)];
titleLab.text = @"我的应用";
titleLab.textColor = [CommonFunctions colorWithHex:0x4A4A4A];
titleLab.font = [UIFont fontWithName:kPingFang_Regular size:14];
[headerView addSubview:titleLab];
}
if (index == 1) {
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 35)];
headerView.backgroundColor = [UIColor whiteColor];
[self addSubview:headerView];
UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 80, 15)];
titleLab.text = @"业务发展";
titleLab.textColor = [CommonFunctions colorWithHex:0x4A4A4A];
titleLab.font = [UIFont fontWithName:kPingFang_Regular size:14];
[headerView addSubview:titleLab];
}
}
- (void)initCollectionFootViewindex:(NSInteger *)indexPath {
UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 10)];
footView.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self addSubview:footView];
}
效果图
以上是关于CollectionView Header And Footer 的使用的主要内容,如果未能解决你的问题,请参考以下文章
使用 Xib 在 Tableview Header 中快速重新加载 CollectionView
ios collectionview 怎么获取header的indexpath