UICollectionView的基本使用

一:创建:

      必须要有一个UICollectionViewLayout来初始化,通常使用UICollectionViewFlowLayout流布局就可以了,有需要就自定义UICollectionViewLayout(比较复杂)。

二:关于cell:

1.全局的重用标识符identifier:cell需要,UICollectionReusableView也需要。

2.注册cell:

    a. 注册代码自定义的cell:

// 注册cell

 [_collectionView_ registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:identifier];

// 注册UICollectionReusableView

[_collectionView_ registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];

[_collectionView_ registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId];


     b.注册xib写的cell:

[_collectionView_ registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:identifier];

3.生成cell:

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

只需要这个方法,就可以生成cell,且不需要判断cell是否存在,如果cell不存在,会自己去生成,然后操作cell的内容就好了。加载xib的cell也是这个方法,这就是与UITableView的区别。

4.cell的大小:遵循UICollectionViewDelegateFlowLayout协议

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

cell之间的间距什么的也都在这个协议的方法内设置。

其余的代理方法与UITableView类似。

三:其他:

a. 设置collectionView横向滑动:

layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

b.设置collectionView不可弹回

collectionView.bounces = NO;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值