美文网首页基础应用
UICollectionView基本使用

UICollectionView基本使用

作者: 小明讲啥故事 | 来源:发表于2019-11-04 11:41 被阅读0次

    1.首先创建 UICollectionViewFlowLayout对象布局。

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
     layout.minimumLineSpacing = 5.0;
     layout.minimumInteritemSpacing = 5.0;
     layout.sectionInset = UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0);
     _myCollectionView.collectionViewLayout = layout;
    

    2.注册collectionViewCell 和头部示图

    [_myCollectionView registerNib:[UINib nibWithNibName:@"CT_MyCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CT_MyCollectionReusableView"];
    //    [_myCollectionView registerClass:[CT_MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CT_MyCollectionReusableView"];
    [_myCollectionView registerNib:[UINib nibWithNibName:@"CT_MyCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"myCollectionViewCell"];
    _myCollectionView.dataSource = self;
    _myCollectionView.delegate = self;
    

    3.实现协议代理

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        return 2;
    }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        if (section == 0) {
            return 15;
        } else if (section == 1) {
            return 12;
        } else {
           return 0;
        }
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        CT_MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCollectionViewCell" forIndexPath:indexPath];
        cell.imag.backgroundColor = [UIColor whiteColor];
        NSString *imagName ;
        if (indexPath.section == 0) {
            imagName = [NSString stringWithFormat:@"Stickers I-01-%02d",indexPath.row+1];
        } else {
            imagName = [NSString stringWithFormat:@"Stickers I-03-%02d",indexPath.row+1];
        }
        cell.imag.image = [UIImage imageNamed:imagName];
        return cell;
    }
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        CGFloat width = ((kScreenW-20)/3);
        CGSize size = CGSizeMake(width, width);
        return size;
    }
    
    
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
        if (kind == UICollectionElementKindSectionHeader) {
            CT_MyCollectionReusableView *aView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CT_MyCollectionReusableView" forIndexPath:indexPath];
            if (indexPath.section == 0) {
                aView.titleLbl.text = @"生活类贴纸展示";
            } else {
                aView.titleLbl.text = @"美食类贴纸展示";
            }
            aView.titleLbl.textColor = [UIColor whiteColor];
            return aView;
        }
        return nil;
    }
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
    {
        if (section == 0) {
            return CGSizeMake(kScreenW, 40);
        }else{
            return CGSizeMake(kScreenW, 40);
        }
    }
    

    4.整个collectionView的头部可以通过设置contentInset实现

        UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, -300,kScreenW,300)];
        [_myCollectionView addSubview:head];
        _myCollectionView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
    

    相关文章

      网友评论

        本文标题:UICollectionView基本使用

        本文链接:https://www.haomeiwen.com/subject/uzwrbctx.html