我们要使用CollectionView里面的头视图需要先注册头视图 UICollectionReusableView或者 继承UICollectionReusableView的子类,kind类型为UICollectionElementKindSectionHeader,并且需要带一个标识符,我们定义个static 的静态字符串就行,如下所示:
[collectionViewregisterClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:headerViewIdentifier];
几个重要的不能忽视的点就是 UICollectionViewFlowLayout 布局属性需要设置 headerReferenceSize头部的大小,不然头视图没有大小不显示;一定要在
- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath; 方法里面创建头视图view并给出frame,然后添加到 UICollectionReusableView 中。
详细代码如下
//
// HomeViewController.m
// collection添加头部
//
// Created by user on 15/10/10.
// Copyright (c) 2015年 user. All rights reserved.
//
#import "HomeViewController.h"
#import "ConstomCell.h"
staticNSString *headerViewIdentifier =@"hederview";
@interface HomeViewController ()
@property (nonatomic,strong)UIImageView *headerImage;
@end
@implementation HomeViewController
- (void)viewDidLoad {
[superviewDidLoad];
//1.添加collectionview
[selfaddCollectionView];
}
-(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,250);//设置collectionView头视图的大小
UICollectionView *collectionView=[[UICollectionViewalloc]initWithFrame:self.view.boundscollectionViewLayout:layout];
collectionView.frame=self.view.bounds;
//注册cell单元格
[collectionViewregisterNib:[UINibnibWithNibName:@"ConstomCell"bundle:nil]forCellWithReuseIdentifier:@"cell"];
//注册头视图
[collectionViewregisterClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:headerViewIdentifier];
collectionView.backgroundColor=[UIColorwhiteColor];
collectionView.delegate=self;
collectionView.dataSource=self;
[self.viewaddSubview:collectionView];
}
#pragma mark 返回多少行
-(NSInteger)collectionView:(UICollectionView *)collectionViewnumberOfItemsInSection:(NSInteger)section
{
return13;
}
#pragma markk 返回的单元格
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionViewcellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ConstomCell *cell=[collectionViewdequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];
return cell;
}
// 返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionViewviewForSupplementaryElementOfKind:(NSString *)kindatIndexPath:(NSIndexPath *)indexPath
{
//如果是头视图
if ([kindisEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *header=[collectionViewdequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:headerViewIdentifierforIndexPath:indexPath];
//添加头视图的内容
[selfaddContent];
//头视图添加view
[headeraddSubview:self.headerImage];
return header;
}
//如果底部视图
// if([kind isEqualToString:UICollectionElementKindSectionFooter]){
//
// }
returnnil;
}
/*
* 补充头部内容
*/
-(void)addContent
{
UIImageView *headerImage=[[UIImageViewalloc]init];
headerImage.contentMode=UIViewContentModeScaleAspectFill;
headerImage.clipsToBounds=YES;
headerImage.frame=CGRectMake(0,0,self.view.frame.size.width,250);
headerImage.image=[UIImageimageNamed:@"mei"];
self.headerImage=headerImage;
}
@end