XZ_iOS之collectionView的header和footer的重用
来源:互联网 发布:网络系统安全的内容 编辑:程序博客网 时间:2024/06/03 12:37
效果图如下,每一个section的头视图是一个label
代码如下:
// 1.创建collectionView
UICollectionViewFlowLayout*flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize=CGSizeMake(W/ 2.0 - 1,40);
flowLayout.minimumInteritemSpacing=1;
flowLayout.minimumLineSpacing=2;
// 设置section的头标题大小
flowLayout.headerReferenceSize=CGSizeMake(W,40);
// flowLayout.footerReferenceSize = CGSizeMake(W, 100);
self.categoryCV= [[UICollectionViewalloc]initWithFrame:[UIScreenmainScreen].boundscollectionViewLayout:flowLayout];
flowLayout.minimumInteritemSpacing=1;
flowLayout.minimumLineSpacing=2;
// 设置section的头标题大小
flowLayout.headerReferenceSize=CGSizeMake(W,40);
// flowLayout.footerReferenceSize = CGSizeMake(W, 100);
self.categoryCV= [[UICollectionViewalloc]initWithFrame:[UIScreenmainScreen].boundscollectionViewLayout:flowLayout];
self.categoryCV.backgroundColor= [UIColorcolorWithWhite:240/255.0alpha:1];
[self.viewaddSubview:self.categoryCV];
// 2.创建一个可重用的section的头视图,继承自UICollectionReusableView
XZCategoryCollectionHeaderView.h中
@interfaceXZCategoryCollectionHeaderView :UICollectionReusableView
@property(nonatomic,strong)UILabel*title;
@property(nonatomic,strong)UILabel*title;
@end
XZCategoryCollectionHeaderView.m中
self.title= [[UILabelalloc]initWithFrame:CGRectMake(15,0,self.bounds.size.width,self.bounds.size.height)];
[selfaddSubview:self.title];
// 3.在controller中注册
staticNSString*headerReuseID = @"collection_header";
//注册UICollectionReusableView的headerView
[self.categoryView.categoryCVregisterClass:[XZCategoryCollectionHeaderViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:headerReuseID];
// 3.在代理方法中调用
#pragma mark - UICollectionViewDelegateFlowLayout
//设置section的头标题
- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath{
UICollectionReusableView *reuseableView = nil;
// 如果是section的头
if (kind == UICollectionElementKindSectionHeader) {
XZCategoryCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:headerReuseIDforIndexPath:indexPath];
NSString *title = self.categoryArr[indexPath.section][@"sTitle"];
headerView.title.text= title;
reuseableView = headerView;
//设置section的头标题
- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath{
UICollectionReusableView *reuseableView = nil;
// 如果是section的头
if (kind == UICollectionElementKindSectionHeader) {
XZCategoryCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:headerReuseIDforIndexPath:indexPath];
NSString *title = self.categoryArr[indexPath.section][@"sTitle"];
headerView.title.text= title;
reuseableView = headerView;
}
returnreuseableView;
}
// 代码部分只是头视图的代码,footer的使用规则一样
0 0
- XZ_iOS之collectionView的header和footer的重用
- RecyclerView的使用(3)之添加Header和Footer
- RecyclerView的使用(3)之添加Header和Footer
- CollectionView Header的使用
- 可以添加Footer和Header的GridView
- 自定义tableview Header和Footer的样式
- RecyclerView添加Header和Footer的基本原理
- RecyclerView添加Header和Footer的方法
- RecyclerView:带header和footer的grid
- RecyclerView优雅的添加Header和Footer
- 设置TableView的header和footer
- 自定义UITableView的Header和Footer
- 打造自己的RecyclerView(一)之添加header和footer
- 去掉UITableView的header和footer的黏性效果
- 如何去掉UITableView中的header和footer的粘连效果
- 修改discuz论坛的标题header和footer
- android ListView 设置header和footer的问题
- 使用RecyclerView实现带Header和Footer的GridView
- Maven xxxx是内部专用 API, 可能会在未来发行版中删除
- 机器学习数据集(Dataset)汇总
- java添加监听器的问题
- angular js 日期时间插件
- socket选项自带的TCP异常断开检测
- XZ_iOS之collectionView的header和footer的重用
- 域名被墙的3种状态
- freemark模板引擎的使用
- jni object的使用
- 设计模式——简单工厂
- mysql到hbase数据迁移之getobject
- 加密算法中BASE64、MD5、SHA、HMAC等之间的区别
- 传教士与野人问题
- js正则表达式