collectionView怎么添加头视图
1个回答
2017-10-25
温州瓯越培训学校
温州市瓯越职业培训学校是一家经温州市民政局批准成立的专门从事培训教育的正规机构,学校自1992年开创教育培训以来不断发展壮大,现开设电脑培训、会计培训、英语培训、手机维修培训等多个培训体系.
向TA提问
关注
展开全部
详细代码如下:
<pre name="code" class="objc">//
// HomeViewController.m
// collection添加头部
//
// Created by user on 15/10/10.
// Copyright (c) 2015年 user. All rights reserved.
//
#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
<pre name="code" class="objc">//
// HomeViewController.m
// collection添加头部
//
// Created by user on 15/10/10.
// Copyright (c) 2015年 user. All rights reserved.
//
#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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询