给UICollectionView添加头视图
#import "PhotoWallCell.h"
#import "PhotoWallModel.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
static NSString *collectionCellIndentider = @"collectionCellIndentider";
@interface ViewController () <UITextFieldDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic ,weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *section1;
@property (nonatomic, strong) NSMutableArray *section2;
@end
- (void)setupSomeParamars
{
_section1 = [NSMutableArray array];
_section2 = [NSMutableArray array];
for(int num = 0;num<5;num++){
PhotoWallModel *model = [[PhotoWallModel alloc] init];
model.photo = [UIImage imageNamed:@"1.png"];
[_section1 addObject:model];
}
for(int num = 0;num<6;num++){
PhotoWallModel *model = [[PhotoWallModel alloc] init];
model.photo = [UIImage imageNamed:@"1.png"];
[_section2 addObject:model];
}
_dataSource = [NSMutableArray arrayWithObjects:_section1,_section2, nil];
}
- (UICollectionView *)collectionView{
if (_collectionView) {
return _collectionView;
}
CGFloat collectionViewHeight = kScreenHeight - 190;
CGRect frame = CGRectMake(0, 150, kScreenWidth, collectionViewHeight);
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.headerReferenceSize = CGSizeMake(320, 50);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
collectionView.opaque = NO;
collectionView.backgroundColor = self.view.backgroundColor;
collectionView.dataSource = self;
collectionView.delegate = self;
collectionView.pagingEnabled = YES;
collectionView.showsVerticalScrollIndicator = NO;
collectionView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:collectionView];
_collectionView = collectionView;
/*!
* @brief 注册cell
*/
[collectionView registerClass:[PhotoWallCell class] forCellWithReuseIdentifier:collectionCellIndentider];
[collectionView registerClass:[PhotoWallCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionCellIndentider];
return collectionView;
}
#pragma mark -
#pragma mark -cell Delegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PhotoWallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCellIndentider forIndexPath:indexPath];
cell.model = self.dataSource[indexPath.section][indexPath.row];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//添加
PhotoWallModel *model = [[PhotoWallModel alloc] init];
model.photo = [UIImage imageNamed:@"1"];
[_section1 addObject:model];
[self.collectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.dataSource[section] count];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return self.dataSource.count;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader){
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionCellIndentider forIndexPath:indexPath];
headerView.backgroundColor = [UIColor redColor];
reusableview = headerView;
}
return reusableview;
}
效果图: