感谢原文:https://blog.csdn.net/wsyx768/article/details/81100584
UITableView的tableHeaderView属性非常好用,而UICollectionView并没有这个属性,所以只有用其它方式实现类似效果。
#import "CollectionViewController.h"
#import "HeaderReusableView.h"
#import "ListCell.h"
@interface CollectionViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) IBOutlet UIView *tvHeaderView;
@end
@implementation CollectionViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self contentInsetHeaderView];
}
#pragma mark - 设置表头方式一
- (void)contentInsetHeaderView {
CGFloat header_y = 200;
// CGFloat top, left, bottom, right;
_collectionView.contentInset = UIEdgeInsetsMake(header_y, 0, 0, 0);
_tvHeaderView.frame = CGRectMake(0, -header_y, [UIScreen mainScreen].bounds.size.width, header_y);
[_collectionView addSubview:_tvHeaderView];
[_collectionView setContentOffset:CGPointMake(0, -header_y)];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"ListCell";
UINib *cellNib = [UINib nibWithNibName:CellIdentifier bundle:nil];
[collectionView registerNib:cellNib forCellWithReuseIdentifier:CellIdentifier];
ListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
#pragma mark - 设置表头方式二
// 要先设置表头大小
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 200);
return size;
}
// 创建一个继承collectionReusableView的类,用法类比tableViewcell
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *reusableView = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
// 头部视图
// 代码初始化表头
// [collectionView registerClass:[HeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderReusableView"];
// xib初始化表头
[collectionView registerNib:[UINib nibWithNibName:@"HeaderReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderReusableView"];
HeaderReusableView *tempHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderReusableView" forIndexPath:indexPath];
reusableView = tempHeaderView;
} else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
// 底部视图
}
return reusableView;
}
@end