美文网首页
UICollectionView

UICollectionView

作者: sunny12 | 来源:发表于2015-11-29 14:50 被阅读454次

UICollectionView主要包括了下面几部分:

UICollectionView-内容显示的主视图,类似于UITableView。

UICollectionViewCell-类似于UITableViewCell在UITableView中。这些cell组成了视图中的内容。

Supplementary Views-如果你除了cell之外,还有信息需要展示,你可以使用supplementary views。通常被用作section的header或者footer

Decoration View-装饰视图,用来提升UICollectionView的背景视觉效果的。

除了上面这些可见的组成部分,UICollectionView还有用来布局内容的不可见组成部分。

UICollectionViewLayout- 用来处理cell在屏幕上的布局。使用一系列的代理方法在UICollectionView上放置cell,而且布局效果是可以在运行时,随时改变的。

创建UICollectionView的常用方法

UICollectionView

1.创建cell以及header,footer

使用代码创建

- registerClass:forCellWithReuseIdentifier:

- registerClass:forSupplementaryViewOfKind:withReuseIdentifier:

使用xib创建

- registerNib:forCellWithReuseIdentifier:

- registerNib:forSupplementaryViewOfKind:withReuseIdentifier:

复用cell

- dequeueReusableCellWithReuseIdentifier:forIndexPath:

- dequeueReusableSupplementaryViewOfKind:

:withReuseIdentifier:forIndexPath:

2.获取Collection View中的Item及位置

- indexPathForItemAtPoint:

- indexPathsForVisibleItems

- indexPathForCell:

- cellForItemAtIndexPath:

3.获取Collection View的状态

- numberOfSections

- numberOfItemsInSection:

UICollectionViewDelegate

1.处理选择的Cells

- collectionView:shouldSelectItemAtIndexPath:

- collectionView:didSelectItemAtIndexPath:

- collectionView:shouldDeselectItemAtIndexPath:

- collectionView:didDeselectItemAtIndexPath:

2.处理Cells的高亮

- collectionView:shouldHighlightItemAtIndexPath:

- collectionView:didHighlightItemAtIndexPath:

- collectionView:didUnhighlightItemAtIndexPath:

UICollectionViewDataSource

1.获取Section和Item的数量

- collectionView:numberOfItemsInSection:

- numberOfSectionsInCollectionView:

2.获取Items的视图

- collectionView:cellForItemAtIndexPath:

- collectionView:viewForSupplementaryElementOfKind:

atIndexPath:

会变魔术的UICollectionViewLayout

UICollectionView之所以可以有千变万化的样式,各种酷帅屌炸天的动画效果,都要归功于UICollectionViewLayout,玩好的UICollectionViewLayout可以实现你想要的各种效果。

UICollectionViewLayout会对屏幕上的cells进行组织布局。

UICollectionViewLayoutAttributes描述了具体如何布局cells, supplementary views, decoration views,具体包括以下属性:

frame -view在collection view的坐标系统中的具体位置

center -view在collection view的坐标系统中的中心

size -view的大小

transform3D -用来旋转,缩放的属性

zIndex -子视图的层级

hidden -view是否隐藏

布局的生命周期

collection view首先调用prepareLayout ,在其中设置布局需要的内部数据结构。这时collection view已经弄清了要展示的数据,但还不知道如何布局,所以你可以在prepareLayout中使用numberOfSections或numberOfItemsInSection:这些方法。然后调用collectionViewContentSize获取content size进行布局。之后调用layoutAttributesForElementsInRect

对可以在屏幕中显示的部分进行布局。layout必须返回屏幕中所有元素的布局属性,包括UICollectionViewCell或者Supplementary Views或者Decoration Views。这时,collectionView了解了足够多的信息去布局cells。当scrollview滑动时,collection view会继续调用layoutAttributesForElementsInRect:去决定新的可见元素的布局。

当下面三种情况发生时,又会调用布局的相关方法。

invalidateLayout -所有布局重新布局

insertItemAtIndexPaths或deleteItemsAtIndexPaths

-这个会发生在用户添加或者移除collection view中的

item时,这时会调用prepareForCollectionViewUpdate:

和finalizeAnimatedBoundsChange:

当collection view的约束改变时 - 调用

prepareForAnimatedBoundsChange和

finalizeAnimatedBoundsChange

由于UICollectionViewLayout是抽象类,所以在布局的时候可以使用Apple提供的UICollectionViewFlowLayout或者自定义Layout

UICollectionViewFlowLayout

这是一个典型的栅格布局,就像上图所以的下厨房的集市页布局。它有以下的一些属性:

scrollDrirection:决定了scroll view的滚动方向

minimumLineSpacing:决定了每行的间隔

minimumIteriItemSpacing:决定了Item之间的间隔

itemSize:决定了Item的大小

sectionInset:section四周的边距

headerReferenceSize:header的大小

footerReferenceSize:footer的大小

自定义UICollectionViewLayout:

自定义UICollectionViewLayout一般是继承UICollectionViewLayout类。重载下列方法:

-(void)prepareLayout:设定一些初始化参数。

-(CGSize)collectionViewContentSize:设定Collection View的尺寸

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect:返回屏幕中所有元素的布局属性

-(UICollectionViewLayoutAttributes _)layoutAttributesForItemAtIndexPath:(NSIndexPath _)indexPath:返回indexPath位置的cell的布局属性

-(UICollectionViewLayoutAttributes _)layoutAttributesForSupplementaryViewOfKind:(NSString _)kind atIndexPath:(NSIndexPath *)indexPath:返回indexPath位置的补充视图的布局属性

-(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString_)decorationViewKind atIndexPath:(NSIndexPath _)indexPath:返回indexPath位置的装饰视图的布局属性

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds:边界改变时,是否重新布局。

3.https://github.com/mokagio/UICollectionViewLeftAlignedLayout

4.https://github.com/ChenYilong/CollectionViewClassifyMenu

5.http://www.jianshu.com/p/22adf62ea491

相关文章

网友评论

      本文标题:UICollectionView

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