【转】UICollectionView使用介绍

UICollectionView 使用介绍



UICollectionView 使用介绍
1.1. Collection View
全家福:
UICollectionView, UITableView, NSCollectionView
不直接等效于NSCollectionView
也不替代UITableView----亲兄弟
为何要使用Collection Views呢?
能够高度定制内容的展示
管理数据最佳的作法
即便是处理大量数据,也很是的高效
咱们先来感性的认识一下Collection Views,下面这幅图就是用Collection Views实现的一个照片墙显示。

1.1.1 Collection View 元素




从上图中,咱们能够看出Collection View的总体构成元素,共有三个要素,分别以下
Cells(单元格)
Supplementary Views(补充的view,至关于TableView的页眉和页脚)
Decoration Views(装饰View,用于装饰整个CollectionView的)
咱们能够分解一下这个照片墙,来描述上面的三个元素都对应什么内容
Cells
以下图,即每一张图片

SupplementaryViews
以下图右边白色的文字部分



Decoration Views
以下图

最终,三个元素,就构成了照片墙,下面是元素构成图



1.1.2 数据模型与交互
数据模型(数据提供者UICollectionViewDataSource)
UICollectionViewDataSource
是一个代理,主要用于向Collection View提供数据。
UICollectionViewDataSource
的主要功能:
Section数目
Section里面有多少item
提供Cellsupplementary view设置
下面咱们依次讲解这些功能。
1
numberOfSectionsInCollectionView:
下图中有多少个sections呢?
答案是2个。即咱们在numberOfSectionsInCollectionView:方法中返回2便可。

2
collectionView:numberOfItemsInSection:
下图中section0中有多少个items呢?
答案是4个。即咱们在collectionView:numberOfItemsInSection:方法中对应的section 0返回4便可。
3
collectionView:cellForItemAtIndexPath:
下图中section0 item 0位置处应该显示什么内容呢?
答案是使用方法collectionView:cellForItemAtIndexPath:返回一个cell,相似下图中的一个view

4
CellView的重用
下面经过5个步骤,来演示CellView的重用
1
)以下图,左边是Collection View,右边是CellView的重用队列,刚开始,左边的数据显示内容,右边的重用队列尚未数据。

2
)再看下图,当用户显示出了Collection View下面的内容后,Collection View中以前的一些CellView就已经再也不被显示了,这是,系统是如何处理呢?

3
)看这里,系统会把不用的CellView添加到重用队列中,以备后面使用。

4
)如何再次被使用呢,请看下图,当用户继续往下看内容的时候,系统就会提供队列中存在的CellView供使用。

5
)最终使用效果以下图



5
iOS6中,Cell重用改善
iOS6中,咱们能够更加方便的使用Cell,系统老是为咱们初始化Cell。咱们能够直接使用。只须要简单的按照两步走便可:
1
 必须使用下面的方法进行Cell类的注册:
- (void)registerClass:forCellWithReuseIdentifier:
- (void)registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
- (void)registerNib:forCellWithReuseIdentifier:
- (void)registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
2
 从队列中取出一个Cell,具体方法以下:
-(id)dequeueReusableCellWithReuseIdentifier:forIndexPath:
-(id)dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
下面咱们经过实际的代码,来演示具体如何进行Cell的重用
第一步:在collection view中进行设置(Cell类的注册)
// In collectionview setup...
[collectionView registerClass:[MyCellclass]
forCellWithReuseIdentifier:@"MY_CELL_ID"]
第二步:在下面的函数中,从队列中取出一个cell便可。而且不再用对cell进行空值判断,以作额外的初始化操做。Cell的一切初始化工做都由系统为咱们作好了。咱们只须要对cell进行一些赋值等操做便可。
-(UICollectionView*)collectionView:(UICollectionView*)cv
cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
MyCell *cell =[cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL_ID"];
if (!cell) {
// Well, nothingreally. Never again
}
// Configure thecell's content
cell.imageView.image= ...
return cell;
}
交互(UICollectionViewDelegate)
UICollectionViewDelegate
的主要功能:
控制cell的高亮
控制cell的选择
cell上支持菜单操做,以下图
                          

选择和高亮在iOS中都有所改进,高亮和flow的精肯定位都很是好控制。
下面列出了经常使用的相关方法,开发者能够参考sdk的帮助文档,进行详细了解。
1
 管理cell的高亮
collectionView:shouldHighlightItemAtIndexPath:
collectionView:didHighlightItemAtIndexPath:
collectionView:didUnhighlightItemAtIndexPath:
这个方法collectionView:shouldHighlightItemAtIndexPath:的效果以下图所示:注意右边selectedhighlighted的值。

这个方法collectionView:didHighlightItemAtIndexPath:的效果以下图所示:注意右边selectedhighlighted的值。

2
 管理cell的选择
collectionView:shouldSelectItemAtIndexPath:
collectionView:didSelectItemAtIndexPath:
collectionView:shouldDeselectItemAtIndexPath:
collectionView:didDeselectItemAtIndexPath:
collectionView:shouldSelectItemAtIndexPath:
的效果以下图

下面两个方法
collectionView:didUnhighlightItemAtIndexPath:
collectionView:didSelectItemAtIndexPath:
的效果以下图所示:

1.1.3 内容的显示
UICollectionViewCell Styles
iOS6
中没有预约义cellStyle
Collection View
跟踪cell的选择和高亮
      
经过设置cellhighlightselection属性(包含子视图)
      
若是进行了相关配置,这能够切换background viewselected background view
咱们来按顺序看下面四幅图。开发者能够自行领悟规律。








下图是UICollectionView相关的类图,从图中咱们能够看到
l UICollectionView
继承自UIScrollView
l
 尊循UICollectionViewDelegateUICollectionViewDataSource两个协议
l
 管理UICollectionViewCell

图中貌似还缺点东西,什么呢?对了,就是缺乏Layout。咱们须要Layoutcell和其它的view进行布局。再看下图,图中多了UICollectionViewLayoutUICollectionViewFlowLayout

下面咱们对UICollectionViewLayout进行介绍
使用本身的layoutUICollectionViewLayout
UICollectionViewLayout
是一个抽象基类,你须要继承自他,来为collection view生成layout信息。Layout对象的做用是决定cellsSupplementary viewsDecorationviewscollection view中的布局位置。
你须要计算以下viewlayout属性
Cells
Supplementary views
Decoration views
系统也为咱们定义了layout属性,即UICollectionViewLayoutAttributes,它主要包括以下内容:
位置
大小
透明度
ZIndex
转场
以下面的两个图是collection viewlayout



技术博客http://www.cnblogs.com/ChenYilong/ 
新浪微博http://weibo.com/luohanchenyilong 

1.2 Flow Layout
1.2.1 核心概念
UICollectionViewFlowLayout
是一个具体的layout对象,用来把item布局在网格中,而且可选页眉和页脚。在collection view中的items,能够从一行或者一列flow至下一行或者下一列(行或者列取决于滚动的方向)。每行都会根据状况,包含尽量多的CellsCells能够是相同的尺寸,也能够是不一样的尺寸。
下面是Flow Layout的一些特性
面向线性布局
可配置为网格
一组lines
具备页眉和页脚

1.2.2 自定义 Flow Layout
Flow Layout能够定制的主要功能以下
Item size
Line spacing
►Inter cell spacing
Scrolling direction
Header and footer size
Section Inset
下面咱们来详细介绍这些可定制的功能。
这里须要对经过全局和delegate定制进行说明一下:
几乎全部的定制属性都是在UICollectionViewFlowLayout中,delegate其实是collection viewdelegateUICollectionViewDelegateFlowLayout只是对UICollectionViewDelegate进行了一些扩展。
Item size(每一个item的大小)
1 能够进行全局配置,以下代码
@property(CGSize)itemSize
layout.itemSize= CGSizeMake(30,20);

2
 也能够经过delegate对每个item进行配置,以下代码
collectionView:layout:sizeForItemAt
IndexPath:
{
return…;
}

效果以下图所示


Line spacing(每行的间距)
1 能够进行全局配置,以下属性
@property(CGFloat) minimumLineSpacing

2
 也能够经过delegate对每个section进行配置,以下代码
ollectionView:layout:minimumLineSpacingForSectionAtIndex:

请按顺序看下面的三个图






Inter cell spacing(每行内部cell item的间距)
1 能够进行全局配置,以下属性
@property(CGFloat) minimumInteritemSpacing

2
 也能够经过delegate对每个section进行配置,以下代码
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:

看看下面的两个图,蓝色是实际的item间距,绿色的是最小item间距。




Scrolling direction(滚动方向)
设置scrollDirection属性便可。两个值以下
UICollectionViewScrollDirectionVertical
UICollectionViewScrollDirectionHorizontal
主要做用:
定义了Flow Layout的基本行为
 控制页眉页脚的维度

UICollectionViewScrollDirectionVertical
效果以下图所示


UICollectionViewScrollDirectionHorizontal
效果以下图所示


Header and footer size(页眉和页脚大小)
下面是页眉和页脚的一些解释。
 也就是supplementary views
 经过数据源的方法来提供内容,以下
-collectionView:viewForSupplementaryElementOfKind:atIndexPath:

 两种常量(类型)
UICollectionElementKindSectionHeader
UICollectionElementKindSectionFooter

 一样须要注册一个类并从队列中取出view
- registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
-registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:

页眉和页脚的size配置方式:
1
)能够全局配置,以下属性
@property(CGSize) headerReferenceSize
@property(CGSize) footerReferenceSize

2
)也能够经过delegate对每个section进行配置,以下代码
collectionView:layout:referenceSizeForHeaderInSection:
collectionView:layout:referenceSizeForFooterInSection:
页眉页脚的属性以下图


当垂直的时候,须要设置Height,以下图


当水平的时候,须要设置Width,以下图


Section Inset
咱们先经过两个图来看看Sections Insets是怎么回事




从上面的两个图中,咱们大概知道了,Section Inset就是某个sectioncell的边界范围。

SectionInset
的配置方式一样有两种
1
 经过全局配置,以下属性
@propertyUIEdgeInsets sectionInset;

2
 也经过delegate对每个section进行配置,以下函数
- (UIEdgeInsets)collectionView:layout:insetForSectionAtIndex:

DEMO





技术博客http://www.cnblogs.com/ChenYilong/ 
新浪微博http://weibo.com/luohanchenyilong


© chenyilong. Powered by Postach.io
相关文章
相关标签/搜索