UICollectionView

// 创建collectView
UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];

// 设置item的大小
flowLaout.itemSize = CGSizeMake(180, 240);

// 设置section之间的间距
flowLaout.sectionInset = UIEdgeInsetsMake(0, 100, 100, 100);

// 设置头分区
flowLaout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);

// 设置脚分区
flowLaout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);

// item之间的间距
flowLaout.minimumInteritemSpacing = 10;

// item 行之间的间距
flowLaout.minimumLineSpacing = 30;

// 改变滑动方向

// flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

// 创建collectionView(并用布局类对象初始化集合视图)
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLaout];

self.collectionView.backgroundColor = [UIColor orangeColor];

self.collectionView.delegate = self;
self.collectionView.dataSource = self;

// 给collectionView注册cell
[self.collectionView registerClass:[RootCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];

// 给collectionView注册头分区的id
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerId"];


// 给collectionView注册脚分区的id
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerId"];


[self.view addSubview:self.collectionView];

pragma mark - 返回头分区和脚分区

  • (UICollectionReusableView )collectionView:(UICollectionView )collectionView viewForSupplementaryElementOfKind:(NSString )kind atIndexPath:(NSIndexPath )indexPath
    {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@”headerId” forIndexPath:indexPath];
    view.backgroundColor = [UIColor yellowColor];

    return view;
    

    } else {
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@”footerId” forIndexPath:indexPath];
    view.backgroundColor = [UIColor brownColor];

    return view;
    

    }
    }

  • (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    {
    return 1;
    }

  • (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
    return self.urlsArray.count;
    }

  • (UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    // RootCollectionViewCell 已经注册给collectionView了所以直接重用就行 , 如果没有.collectionView会自己来的创建,不用我们管创建过程
    RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@”cellId” forIndexPath:indexPath];

    cell.label.text = [NSString stringWithFormat:@”贝爷的第%ld个小妾” , indexPath.row + 1];

    NSString *urlString = self.urlsArray[indexPath.row];
    cell.imageView.imageURL = [NSURL URLWithString:urlString];

// cell.backgroundColor = [UIColor cyanColor];
return cell;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值