iOS 深入学习UICollectionView
前言: 这是博主在学习旧知识笔记中的一篇, 谁叫咱入门晚呢, 学习的过程中, 不会的知识, 就是因为我们年轻而已(囧~) 我总是这样安慰自己(逃~), 所以我们需要补充一下”错过”的重要的东西, 下面列出了本文会提到的一些概念. 如不感兴趣直接退出即可
uicollectionview, uicollectionviewdatasource, uicollectionviewdelegate, uicollectionviewdelegateflowlayout, uicollectionviewlayout
在开始之前先有必要回忆一下uicollectionview
使用collectionview有一段时间了, 进行一个简单的总结和理解~~~.
uicollectionview是一种类似于uitableview不同于uitableview的布局方式.
cells 用于展示内容的主体,对于不同的cell可以指定不同尺寸和不同的内容, 且可以复用(一般需要自定义 没有提供类似于tableview那么多的属性). supplementary views 追加视图 可以理解为tableview每个section的header或者footer,用来标记每个section的view, 且也可以被复用. decoration views 装饰视图 这是每个section的背景, 且也可以被复用.
关于cell的说明:
- 首先是cell本身作为容器view
- 然后是一个大小自动适应整个cell的backgroundview,用作cell平时的背景
- 再其上是selectedbackgroundview,是cell被选中时的背景
- 最后是一个contentview,自定义内容应被加在这个view上
uicollectionviewdatasource
// 多少个items - (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section; // 显示cell - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath; // 多少个sections - (nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview;
实现上面三个方法, 基本就可以正常工作了.
uicollectionviewdelegate
进行一些行为操作时候用到的
- cell的高亮
- cell的点击
- cell的选中状态等等.
对于点击cell发生哪些行为做一个说明
1collectionview:shouldhighlightitematindexpath: 是否应该高亮?
2?collectionview:didhighlightitematindexpath: 如果1回答为是,那么高亮
3collectionview:shouldselectitematindexpath: 无论1结果如何,都询问是否可以被选中?
4collectionview:didunhighlightitematindexpath: 如果1回答为是,那么现在取消高亮
5collectionview:didselectitematindexpath: 如果3回答为是,那么选中cell
uicollectionviewdelegateflowlayout
可以对cell的大小, 间隙进行调整 使用协议的方式
uicollectionviewflowlayout
是uicollectionviewlayout(稍后会提到)的子类, 他提供一些对cell进行简单设置的属性, flow layout简单说是一个直线对齐的layout.
// 系统api提供的一些属性 @property (nonatomic) cgfloat minimumlinespacing; @property (nonatomic) cgfloat minimuminteritemspacing; @property (nonatomic) cgsize itemsize; @property (nonatomic) cgsize estimateditemsize ns_available_ios(8_0); // defaults to cgsizezero - setting a non-zero size enables cells that self-size via -preferredlayoutattributesfittingattributes: @property (nonatomic) uicollectionviewscrolldirection scrolldirection; // default is uicollectionviewscrolldirectionvertical @property (nonatomic) cgsize headerreferencesize; @property (nonatomic) cgsize footerreferencesize; @property (nonatomic) uiedgeinsets sectioninset; // set these properties to yes to get headers that pin to the top of the screen and footers that pin to the bottom while scrolling (similar to uitableview). @property (nonatomic) bool sectionheaderspintovisiblebounds ns_available_ios(9_0); @property (nonatomic) bool sectionfooterspintovisiblebounds ns_available_ios(9_0);
?uicollectionviewlayout
它负责了将各个cell、supplementary view和decoration views进行组织,为它们设定各自的属性.可以有:位置, 尺寸, 透明, 层级, 形状, 等等. 所以自定义各种样式的布局就需要自定义layout了.
实现一个自定义layout一般继承于uicollectionviewlayout然后需要重写以下几个方法:
// 返回collectionview的内容尺寸 -(cgsize)collectionviewcontentsize // 返回rect中的所有的元素的布局属性, 返回的是包含uicollectionviewlayoutattributes的nsarray, uicollectionviewlayoutattributes可以是cell, supplementary view和decoration view -(nsarray *)layoutattributesforelementsinrect:(cgrect)rect // 返回对应于indexpath的位置的cell的布局属性 -(uicollectionviewlayoutattributes _)layoutattributesforitematindexpath:(nsindexpath _)indexpath // 返回对应于indexpath的位置的追加视图的布局属性 -(uicollectionviewlayoutattributes _)layoutattributesforsupplementaryviewofkind:(nsstring _)kind atindexpath:(nsindexpath *)indexpath // 返回对应于indexpath的位置的装饰视图的布局属性 -(uicollectionviewlayoutattributes * )layoutattributesfordecorationviewofkind:(nsstring_)decorationviewkind atindexpath:(nsindexpath _)indexpath: // 默认返回yes, 边界发生变化时, 重新进行布局. - (bool)shouldinvalidatelayoutforboundschange:(cgrect)newbounds:
注意: uicollectionviewlayout实例化之后, 有一些方法将自动被调用
// 一般在该方法中设定一些必要的layout的结构和初始需要的参数 -(void)preparelayout // 处置collection应该占据的尺寸(所有内容所占的尺寸) collectionview的本质是一个scrollview, 需要滚动尺寸 -(cgsize) collectionviewcontentsize
需要更新layout时, 调用这个方法 有点uiview的setneedslayout的意思…
- invalidatelayout
说了这么多, 我觉得我理解的更深一点了, 你呢?
说的再多不如show you my demo 稍后有时间补上… [大笑.png]
这里是官方的一个dmeohttps://github.com/mpospese/circlelayout.git
end
上一篇: 半小时入门Thrift