我們都知道UITableView有個(gè)ViewForHeaderInSection
然后我們?cè)谧鯱ICollectionView的時(shí)候同樣需要一個(gè)SectionHeaderView
首先需要自定義一個(gè)view
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface SSChoosePhotoHeaderView : UICollectionReusableView
@property (nonatomic, strong)UILabel* testLabel;
@end
NS_ASSUME_NONNULL_END
#import "SSChoosePhotoHeaderView.h"
@implementation SSChoosePhotoHeaderView
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, KMAIN_SCREEN_WIDTH, 44)];
_testLabel = label;
[self addSubview:label];
}
return self;
}
然后呢需要在CollectionView去注冊(cè), 并實(shí)現(xiàn)代理方法, 重點(diǎn):必須在Layout的HeaderReferenceSize里給個(gè)size
-(void)setBasicUI{
CGFloat kItemWidth = (self.view.width-6)/3;
CGFloat kItemHeight = kItemWidth;
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
[layout setItemSize:CGSizeMake(kItemWidth, kItemHeight)];
// [layout setSectionInset:UIEdgeInsetsMake(2, 2, 2 ,2)];
[layout setSectionInset:UIEdgeInsetsMake(1, 1, 1 , 1)];
layout.headerReferenceSize = CGSizeMake(KMAIN_SCREEN_WIDTH, 44);
self.collectionView = [[UICollectionView alloc]initWithFrame:(CGRect){0, 0, self.view.width, self.view.height} collectionViewLayout:layout];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
self.collectionView.alwaysBounceVertical = YES;
[self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([SSChoosePhotoCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([SSChoosePhotoCollectionViewCell class])];
[self.view addSubview:self.collectionView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.naviBar.mas_bottom);
make.left.right.bottom.equalTo(self.view);
}];
[_collectionView registerClass:[SSChoosePhotoHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([SSChoosePhotoHeaderView class])];
if ([self.collectionView respondsToSelector:@selector(setPrefetchingEnabled:)]) {
self.collectionView.prefetchingEnabled = false;
}
// [self.collectionView reloadData];
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
SSChoosePhotoHeaderView* view = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([SSChoosePhotoHeaderView class]) forIndexPath:indexPath];
if (view == nil) {
view = [[SSChoosePhotoHeaderView alloc] initWithFrame:CGRectMake(0, 0, KMAIN_SCREEN_WIDTH, 44)];
}
JSDateModel* model = _itemsArray[indexPath.section];
view.testLabel.text = model.dateString;
return view;
}
return nil;
}
接下來就是如果headsize有所變化的話就必須實(shí)現(xiàn)代理方法UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;//item尺寸
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;//section Inset
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;//行間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;//列間距
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;//head size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;//footer size