collectionView头视图与组视图的创建
2015-11-04 14:31
351 查看
//1.头视图的创建
staticNSString *headerViewIdentifier =@"hederview";
-(void)addCollectionView
{
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayoutalloc]init];
layout.minimumLineSpacing=20;//设置每一行的间距
layout.itemSize=CGSizeMake(100,100);
//设置每个单元格的大小
layout.sectionInset=UIEdgeInsetsMake(0,0,
50, 0);
layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width,200);
//设置collectionView头视图的大小
UICollectionView *collectionView=[[UICollectionViewalloc]initWithFrame:self.view.boundscollectionViewLayout:layout];
collectionView.frame=self.view.bounds;
//注册cell单元格
[collectionView registerNib:[UINibnibWithNibName:@"ConstomCell"bundle:nil]forCellWithReuseIdentifier:@"cell"];
//注册头视图
[collectionView registerClass:[UICollectionReusableViewclass]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:headerViewIdentifier];
collectionView.backgroundColor=[UIColorwhiteColor];
collectionView.delegate=self;
collectionView.dataSource=self;
[self.viewaddSubview:collectionView];
}
#pragma mark 返回多少行
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return
13;
}
#pragma markk 返回的单元格
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath
4000
{
ConstomCell *cell=[collectionViewdequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];
return cell;
}
// 返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath
{
//如果是头视图
if ([kindisEqualToString:UICollectionElementKindSectionHeader])
{
UICollectionReusableView *header=[collectionViewdequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:headerViewIdentifierforIndexPath:indexPath];
//添加头视图的内容
[selfaddContent];
//头视图添加view
[headeraddSubview:self.headerImage];
return header;
}
//如果底部视图
// if([kind isEqualToString:UICollectionElementKindSectionFooter]){
//
// }
return
nil;
}
/*
* 补充头部内容
*/
-(void)addContent
{
UIImageView *headerImage=[[UIImageViewalloc]init];
headerImage.contentMode=UIViewContentModeScaleAspectFill;
headerImage.clipsToBounds=YES;
headerImage.frame=CGRectMake(0,0,
self.view.frame.size.width,200);
headerImage.image=[UIImageimageNamed:@"mei"];
self.headerImage=headerImage;
}
详细demo地址 点击打开链接
//2.组视图的创建
//先创建
ReusableView类 继承于UICollectionReusableView
然后collectionView注册
[selfregisterNib:[UINibnibWithNibName:@"ManagerReusableView"bundle:nil]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"ManagerReusableView"];
//协议方法里面创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath{
ManagerReusableView *header = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"ManagerReusableView"forIndexPath:indexPath];
return header;
}
//设置宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return
CGSizeMake(kScreenWidth,30);
}
collectionView组视图demo下载连接
staticNSString *headerViewIdentifier =@"hederview";
-(void)addCollectionView
{
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayoutalloc]init];
layout.minimumLineSpacing=20;//设置每一行的间距
layout.itemSize=CGSizeMake(100,100);
//设置每个单元格的大小
layout.sectionInset=UIEdgeInsetsMake(0,0,
50, 0);
layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width,200);
//设置collectionView头视图的大小
UICollectionView *collectionView=[[UICollectionViewalloc]initWithFrame:self.view.boundscollectionViewLayout:layout];
collectionView.frame=self.view.bounds;
//注册cell单元格
[collectionView registerNib:[UINibnibWithNibName:@"ConstomCell"bundle:nil]forCellWithReuseIdentifier:@"cell"];
//注册头视图
[collectionView registerClass:[UICollectionReusableViewclass]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:headerViewIdentifier];
collectionView.backgroundColor=[UIColorwhiteColor];
collectionView.delegate=self;
collectionView.dataSource=self;
[self.viewaddSubview:collectionView];
}
#pragma mark 返回多少行
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return
13;
}
#pragma markk 返回的单元格
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
*)indexPath
4000
{
ConstomCell *cell=[collectionViewdequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];
return cell;
}
// 返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath
{
//如果是头视图
if ([kindisEqualToString:UICollectionElementKindSectionHeader])
{
UICollectionReusableView *header=[collectionViewdequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:headerViewIdentifierforIndexPath:indexPath];
//添加头视图的内容
[selfaddContent];
//头视图添加view
[headeraddSubview:self.headerImage];
return header;
}
//如果底部视图
// if([kind isEqualToString:UICollectionElementKindSectionFooter]){
//
// }
return
nil;
}
/*
* 补充头部内容
*/
-(void)addContent
{
UIImageView *headerImage=[[UIImageViewalloc]init];
headerImage.contentMode=UIViewContentModeScaleAspectFill;
headerImage.clipsToBounds=YES;
headerImage.frame=CGRectMake(0,0,
self.view.frame.size.width,200);
headerImage.image=[UIImageimageNamed:@"mei"];
self.headerImage=headerImage;
}
详细demo地址 点击打开链接
//2.组视图的创建
//先创建
ReusableView类 继承于UICollectionReusableView
然后collectionView注册
[selfregisterNib:[UINibnibWithNibName:@"ManagerReusableView"bundle:nil]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"ManagerReusableView"];
//协议方法里面创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath{
ManagerReusableView *header = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"ManagerReusableView"forIndexPath:indexPath];
return header;
}
//设置宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return
CGSizeMake(kScreenWidth,30);
}
collectionView组视图demo下载连接
相关文章推荐
- python问题之requests\packages\urllib3\util\ssl_.py:100: InsecurePlatformWarning
- JVM系列三:JVM参数设置、分析
- [OpenJudge-NOI]棋盘问题 回溯
- Python的@修饰符和装饰器
- C语言结构体内存对齐
- 實戰案例 - 資料對接工具程式碼重構 (1)
- EC读书笔记系列之7:条款12 复制对象时勿忘其每一个成分
- 2015-11-04 报表 (asp.net 部分)
- 广播发送与接受的两种方式
- adb命令安装模拟器上的Android程序,节省了很多时间。。
- 研究Xposed相关二:如何root android模拟器(android4.3.1)
- php三元运算符 ? :
- VC 使用combox
- JVM系列二:GC策略&内存申请、对象衰老
- UI界面设计嘚吧嘚
- [OpenJudge-NOI]滑雪 记忆化搜索+Dp
- 接口-抽象,虚方法
- Coreseek 3.2.14 searchd gdb breakpoints
- PHP使用eval()函数的注意事项
- 学习嵌入式Linux-JZ2440-初识Bootloader