UICollectionViewController的使用

分享一下collectionviewcontroller的用法,不足之处请大家补充

static NSString *const kItemIdentity = @"cellId";

1.init生成controller时需要生成相应的layout,否则报错

- (instancetype)init
{
    //创建一个流式布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置每个cell的大小
    layout.itemSize = CGSizeMake(80, 80);
    //设置每个cell间的最小水平间距
    layout.minimumInteritemSpacing = 0;
    //设置每个cell间的行间距
    layout.minimumLineSpacing = 5;
    //设置每一组距离四周的内边距
    layout.sectionInset = UIEdgeInsetsMake(5, 0, 0, 0);
    layout.headerReferenceSize = CGSizeMake(0, 40);
    layout.footerReferenceSize = CGSizeMake(0, 40);

    //返回
    return [super initWithCollectionViewLayout:layout];
}

2.注册item、区头、区尾等视图

    
    /*
     *这是重点 必须注册cell
     */
    //这种是xib建的cell 需要这么注册
    UINib *cellNib=[UINib nibWithNibName:@"FilterUserCollectionViewCell" bundle:nil];
    [_collectionView registerNib:cellNib forCellWithReuseIdentifier:@"FilterUserCollectionViewCell"];
    //这种是自定义cell不带xib的注册
    //   [_collectionView registerClass:[CollectionViewCell1 class] forCellWithReuseIdentifier:@"myheheIdentifier"];
    //这种是原生cell的注册
    //    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

 //注册cell
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kItemIdentity];
    
    //注册区头视图
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    //注册区尾视图
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

3.当每个区的区头以及每个区的区尾高度不同时我们可以通过一下两个代理方法解决,不过在此之前必须有   layout.headerReferenceSize = CGSizeMake(0, 40);
layout.footerReferenceSize = CGSizeMake(0, 40);

否则代理方法不走:

//当每个区的区头高度不一样时通过此方法设置区头高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(0, 100);
    }
    return CGSizeMake(0, 0.001);
}

//当每个区的区尾高度不一样是通过该方法设置高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(0, 0.001);
    }
    return CGSizeMake(0, 40);
}

/**
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
**/

4.设置每个区的item大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //返回每个item的大小
    if (indexPath.section == 0) {
        return CGSizeMake(200, 200);
    }
    return CGSizeMake(100, 100);
}

5.设置区头和区尾视图

//设置区头区尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind: kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor lightGrayColor];
        return headerView;
    }
    else
    { UICollectionReusableView * footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor purpleColor];
        return footerView;
    }
}

6.在代理方法中设置相应的数据源

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}


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

    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kItemIdentity forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}

不足之处请大家多多补充

转载于:https://my.oschina.net/huqiji/blog/834330

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值