给UICollectionView添加头视图

本文介绍如何在UICollectionView中添加头部视图,并展示了完整的代码实现。包括UICollectionView的设置、注册单元格和头部视图、填充数据等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

    

    

    

}

效果图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值