UICollectionView创建头部headerview
2017-07-06 15:06
78 查看
使用自带的头部视图
#import "HomeViewController.h"
#import "ConstomCell.h"
static NSString *headerViewIdentifier = @"hederview";
@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UIImageView *headerImage;
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.添加collectionview
[self addCollectionView];
}
-(void)addCollectionView
{
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
layout.minimumLineSpacing=20; //设置每一行的间距
layout.itemSize=CGSizeMake(100, 100); //设置每个单元格的大小
layout.sectionInset=UIEdgeInsetsMake(0, 0, 50, 0);
layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 250); //设置collectionView头视图的大小
UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
collectionView.frame=self.view.bounds;
//注册cell单元格
[collectionView registerNib:[UINib nibWithNibName:@"ConstomCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
//注册头视图
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];
collectionView.backgroundColor=[UIColor whiteColor];
collectionView.delegate=self;
collectionView.dataSource=self;
[self.view addSubview:collectionView];
}
#pragma mark 返回多少行
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 13;
}
#pragma markk 返回的单元格
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ConstomCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
return cell;
}
// 返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//如果是头视图
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];
//添加头视图的内容
[self addContent];
//头视图添加view
[header addSubview:self.headerImage];
return header;
}
//如果底部视图
// if([kind isEqualToString:UICollectionElementKindSectionFooter]){
//
// }
return nil;
}
/*
* 补充头部内容
*/
-(void)addContent
{
UIImageView *headerImage=[[UIImageView alloc]init];
headerImage.contentMode=UIViewContentModeScaleAspectFill;
headerImage.clipsToBounds=YES;
headerImage.frame=CGRectMake(0, 0, self.view.frame.size.width, 250);
headerImage.image=[UIImage imageNamed:@"mei"];
self.headerImage=headerImage;
}
@end 自定义headerview
//头顶部
[self.collectionView registerClass:[OnlineChantHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderViewId];
#import "HomeViewController.h"
#import "ConstomCell.h"
static NSString *headerViewIdentifier = @"hederview";
@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UIImageView *headerImage;
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.添加collectionview
[self addCollectionView];
}
-(void)addCollectionView
{
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
layout.minimumLineSpacing=20; //设置每一行的间距
layout.itemSize=CGSizeMake(100, 100); //设置每个单元格的大小
layout.sectionInset=UIEdgeInsetsMake(0, 0, 50, 0);
layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 250); //设置collectionView头视图的大小
UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
collectionView.frame=self.view.bounds;
//注册cell单元格
[collectionView registerNib:[UINib nibWithNibName:@"ConstomCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
//注册头视图
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];
collectionView.backgroundColor=[UIColor whiteColor];
collectionView.delegate=self;
collectionView.dataSource=self;
[self.view addSubview:collectionView];
}
#pragma mark 返回多少行
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 13;
}
#pragma markk 返回的单元格
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ConstomCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
return cell;
}
// 返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//如果是头视图
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];
//添加头视图的内容
[self addContent];
//头视图添加view
[header addSubview:self.headerImage];
return header;
}
//如果底部视图
// if([kind isEqualToString:UICollectionElementKindSectionFooter]){
//
// }
return nil;
}
/*
* 补充头部内容
*/
-(void)addContent
{
UIImageView *headerImage=[[UIImageView alloc]init];
headerImage.contentMode=UIViewContentModeScaleAspectFill;
headerImage.clipsToBounds=YES;
headerImage.frame=CGRectMake(0, 0, self.view.frame.size.width, 250);
headerImage.image=[UIImage imageNamed:@"mei"];
self.headerImage=headerImage;
}
@end 自定义headerview
//头顶部
[self.collectionView registerClass:[OnlineChantHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderViewId];
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableView = nil; if (kind == UICollectionElementKindSectionHeader) {//藏书 定制头部视图的内容 } OnlineChantHeaderView *headerView = (OnlineChantHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderViewId forIndexPath:indexPath]; reusableView = headerView; Model *tempModel = self.stackRoomArray[indexPath.section]; headerView.name = tempModel.Name; return reusableView; }
相关文章推荐
- iOS6开发----应用集合视图(UICollectionView)-创建UICollectionViewCell子类单元格
- 应用集合视图(UICollectionView)-创建UICollectionViewCell子类单元格
- UICollectionView Section 纯代码代码控制 HeaderView和FooterView的显示
- UICollectionView的header悬停
- [iOS]swift之UITableView添加通过xib创建的headerView坑爹问题
- 应用集合视图(UICollectionView)-创建基于Storyboard的集合视图应用程序
- 怎样在UICollectionView中添加Header和footer
- UICollectionViewController的创建
- UICollectionView 简单使用 代码创建
- StroyBoard中UICollectionView中添加Header和footer
- iOS6 UICollectionView(代码创建和nib)
- [转]通过UICollectionView创建网格布局
- Storyboard创建使用UICollectionViewController
- swift UICollectionView 代码创建 和自定义Cell
- Storyboard创建使用UICollectionViewController
- iOS6开发----应用集合视图(UICollectionView)-创建基于Storyboard的集合视图应用程序
- Storyboard创建使用UICollectionViewController
- UITableView SectionHeader 自定义section的头部
- ios8 Swift UICollectionView Header 常驻(如UITableView 的Header效果)实现