IOS-UICollectionView的基本使用以及添加headerView

本文介绍UICollectionView的基本使用方法,并详细讲解如何为其添加HeaderView以实现类似UITableView的头部视图效果。包括UICollectionView的初始化、数据源及代理方法的设置等。

UICollectionView 说白了就是照片墙

在展示类的App中也很常见,比如:瀑布流。

下面说一下如何简单的使用 和如何添加headerView 让他也拥有类似于tableview的headerView的效果

UIKIT_EXTERN NSString *const UICollectionElementKindSectionHeader;  //定义好Identifier 

static NSString *const HeaderIdentifier = @"HeaderIdentifier";



 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];  //此处自动布局,我没有写任何东西 布局样式都在cell中

    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-70) collectionViewLayout:layout];


    self.collectionView.dataSource = self;  

    self.collectionView.delegate = self;   

    self.collectionView.showsVerticalScrollIndicator = NO;

    //此处需要注册才可以使用  

    [self.collectionView registerClass:[DiaperColectionCell class] forCellWithReuseIdentifier:BrankViewCellIdentifier]; 

    [self.collectionView registerClass:[MYBrandHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderIdentifier];

    [self.view addSubview:self.collectionView];


下面执行代理方法 跟tableview 一样的

#pragma mark -

#pragma mark collectionViewDelegate

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    //section数量

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    //数据数量

}

//设置元素大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    

    return (返回一个Item的 宽高)

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

//此处就是自定义好的cell

}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

//UICollectionView被选中时调用的方法

}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

    MYBrandHeaderView *headReusableView;

//此处是headerView

    if (kind == UICollectionElementKindSectionHeader) {

        headReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HeaderIdentifier forIndexPath:indexPath];

        headReusableView.frame = CGRectZero;

        headReusableView.delegate = self;

        headReusableView.modeNumber = 1;

        [((MYBrandHeaderView *)headReusableView) setData:self.headerDict];

    }

    return headReusableView;

}


//执行的 headerView 代理  返回 headerView 的高度

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{

    CGFloat height = [self.topView getHeight];

    return CGSizeMake(320, height);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值