#import "Cell.h"
static NSString *ident = @"Cell";
#import "HeaderCollectionReusableView.h"
static NSString *head = @"HeaderCollectionReusableView";
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_collectionView registerNib:[UINib nibWithNibName:ident bundle:nil] forCellWithReuseIdentifier:ident];
[_collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:head];
_arr=@[@"111",@"222",@"111",@"222",@"111",@"222",@"111",@"222",@"111",@"222",@"111",@"222",@"111",@"222"].mutableCopy;
}
#pragma mark ————————— 分区个数 —————————————
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return _arr.count;
}
#pragma mark ————————— Header的大小 size —————————————
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(self.view.frame.size.width, 30);
}
#pragma mark ————————— 自定义分区头 —————————————
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader){
HeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:head forIndexPath:indexPath];
[headerView getSHCollectionReusableViewHearderTitle:_arr[indexPath.section]];
headerView.titleLabel.textColor = [UIColor grayColor];
reusableview = headerView;
}
return reusableview;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 10;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ident forIndexPath:indexPath];
return cell;
}
#import <UIKit/UIKit.h>
@interface HeaderCollectionReusableView : UICollectionReusableView
@property (nonatomic,strong)UILabel *titleLabel;
/**
* 声明相应的数据模型属性,进行赋值操作,获取头视图或尾视图需要的数据.或者提供一个方法获取需要的数据.
*/
-(void)getSHCollectionReusableViewHearderTitle:(NSString *)title;
@end
#import "HeaderCollectionReusableView.h"
@implementation HeaderCollectionReusableView
-(id)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
[self createBasicView];
}
return self;
}
/**
* 进行基本布局操作,根据需求进行.
*/
-(void)createBasicView{
_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 0,self.frame.size.width-50, self.frame.size.height)];
_titleLabel.textColor = [UIColor whiteColor];
[self addSubview:_titleLabel];
}
/**
* 设置相应的数据
*
* @param title
*/
-(void)getSHCollectionReusableViewHearderTitle:(NSString *)title{
_titleLabel.text=title;
}
@end