UICollectionView自定义布局

本文详细介绍了UICollectionView的两种布局方式:UICollectionViewFlowLayout线性布局和UICollectionViewLayout自定义布局。包括如何使用代理控制cell大小,自定义补充视图及装饰视图等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个和 UItableView 在用法上大体上差不多这里只说明一下 UICollectionView 的布局。 demo

一. UICollectionViewFlowLayout布局方式:

   这种布局方式是一种线性布局,他会先按照你设置的要求铺慢一行,当不满足指定条件的时候,会换行。
  我们可以通过  UICollectionViewDelegateFlowLayout  代理来控制没个 cell 和 supplementary view 的大小。
  但我觉得这种布局方式,很难满足我的要求,因此我很少用这个。

二.继承 UICollectionViewLayout 的布局方式:

Layout 对象需要用到 CollectionView 的 datasource,其通过自身的 collectionView 属性来获得 datasource。
collectionView 完全通过 Layout 对象来管理布局。当我们移动、删除、添加 item 时我们可以通过 invaildateLayout 
来重新生成 布局,如果我们只是想更新 item 上的数据直接 reloadData 就可以了。
当开始布局的时候,系统会依次调用如下三个最主要的方法:
1.prepareLayout 我们可以在里面提供布局前的准备工作

2.  collectionViewContentSize 这个方法控制 CollectionView 的显示范围

3.  layoutAttributesForElementsInRect:这个方法会告诉 collectionView 在 Rect 这个范围内 item的  UICollectionViewLayoutAttributes对象, 这个对象记录了 item的size和position,fram,其他的一些属性可以控制 item 的外观
在这个方法里首先调用如下三个方法创建不同视图对应 UICollectionViewLayoutAttributes对象:

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;

-(UICollectionViewLayoutAttributes*)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath

-(UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath


   UICollectionViewLayoutAttributes的对象的创建方法:

    layoutAttributesForCellWithIndexPath:
     layoutAttributesForSupplementaryViewOfKind:withIndexPath
    layoutAttributesForDecorationViewOfKind:withIndexPath   
   注意要根据 item 的类型不同选择正确的对象创建方法,否则的话会出错。
    总而言之我们要在这个方法里做如下事情:
   1)判断有多少个 item 在 Rect 区域里
   2)为每个在这个区域里的 item 生成 UICollectionViewLayoutAttributes对象,并添加到数组里

collection view会在正常的layout 过程之外周期性地让你提供单个items的layout对象。比如为某item配置插入和删除动画时。返回属性时,不能更新这些layout属性,如果需要改变layout信息,调用invalidateLayout,在接下来的layout周期中更新这些信息。上述方法中layoutAttributesForItemAtIndexPath:是所有自定义 layout都必须重载的方法,如果有supplementary view和decoration view可以分别重载下面两个方法。


三.自定义supplementary views和Cells

添加supplementary viewlayout中的过程如下:

    1 注册supplementary viewlayout对象中,registerClass:forSupplementaryViewOfKind:withReuseIdentifier: or   registerNib:forSupplementaryViewOfKind:withReuseIdentifier:

   2 datasource中实现collectionView:viewForSupplementaryElementOfKind:atIndexPath:,由于这些view是可重用的,调用dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:来获取可用的view

cell 的创建类似,cell 和 supplementary view唯一不同的是 cell 只能有一个supplementary view可以有多个。

四.Decoration Views的创建

Decoration Viewslayout UI特征的有效点缀,与cellsupplementary view不同的是,它只做外观呈现用,所以与datasource无关。可以用来提供自定义背影,在Cells缝隙之间填充,甚至可以掩盖cell,它完全由layout对象控制。

layout中添加Decoration view步骤如下:

   1 registerClass:forDecorationViewOfKind: or registerNib:forDecorationViewOfKind: method方法注册自定义的decoration view,但记住是在layout对象中注册

   2 layout对象中layoutAttributesForElementsInRect:方法中为decoration view创建属性

   3 实现layoutAttributesForDecorationViewOfKind:atIndexPath:方法并在请求时返回decoration view的布局属性



基于以上内容写了一个超级课程表: https://github.com/mrHuangWenHai/--CollectionView-Course-List.git


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值