0. 简介

  参考:支持重排的iOS9 UICollectionView

  参考:http://www.cnblogs.com/langtianya/p/3902801.html

  参考:http://www.cnblogs.com/ios8/p/iOS-UICollectionView.html

  UIICollectionView 必须实现UICollectionDataSource、UICollectionViewDelegate、UICollectionViewDelegateFlowLayout这三个协议。

0.1. 相似控件

   UICollectionView, UITableView, NSCollectionView

0.2. UICollectionView的优势

  • 高度定制内容
  • 优秀的管理数据的能力
  • 高效处理大量数据

0.3. 构成

0.3.1. 单元格

  Cells,主要的显示视图。

0.3.2. 补充View

  Supplementary Views,页眉页脚。

0.3.3. 装饰View

  Decoration Views,背景效果。

1. 常用协议

1.1. Section的个数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

1.2. 每个Section里Item的个数

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

1.3. 每个UICollectionViewCell展示的内容

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString * CellIdentifier = @"GradientCell";

    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.backgroundColor = [UIColor colorWithRed:(( * indexPath.row) / 255.0) green:(( * indexPath.row)/255.0) blue:(( * indexPath.row)/255.0) alpha:1.0f];

    return cell;

  }

1.4. 每个UICollectionViewCell的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(, );
}

1.5. 每个UICollectionView Section的margin

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section  {
return UIEdgeInsetsMake(, , , );
}

1.6. UICollectionViewCell选中时

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
}

1.7. UICollectionView是否可以被选中

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath  {
return YES;
}

2. 重用

2.1. 注册Cell类

- (void)registerClass:forCellWithReuseIdentifier:

- (void)registerClass:forSupplementaryViewOfKind:withReuseIdentifier:

- (void)registerNib:forCellWithReuseIdentifier:

- (void)registerNib:forSupplementaryViewOfKind:withReuseIdentifier:

2.2. 从队列取出Cell

-(id)dequeueReusableCellWithReuseIdentifier:forIndexPath:

-(id)dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:

2.3.1. 示例代码

2.3.1.1. 注册

  在CollectionView类中注册Cell类:

[collectionView registerClass:[MyCellclass] forCellWithReuseIdentifier:@”MY_CELL_ID”]

2.3.1.2. 使用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; }

3. 内容显示

  UICollectionView继承自UIScrollView,UICollectionView通过UICollectionViewLayout和UICollectionViewFlowLayout对cell和其它view进行布局。

  UICollectionViewCell由三部分组成:

  • Background View
  • Selected Background View
  • Content View

3.1. UICollectionViewLayout

3.1.1. 使用自定义的Layout(UICollectionViewLayout)

  这是一个抽象基类,需要继承自它,来为collection view 生成layout信息。Layout对象的作用是决定cells、Supplementary views和 Decorationviews在collection view中的布局。需要计算这些view的layout属性。

3.1.2. 使用系统Layout(UICollectionViewLayoutAttributes)

  主要包含:

  • 位置
  • 大小
  • 透明度
  • ZIndex
  • 转场

3.2. Flow Layout

3.2.1. 核心概念

  UICollectionViewFlowLayout是一个具体的layout对象,用来把Items布局在网格中,并且可选页眉和页脚。在collection view中的Items,可以从一行或一列flow至下一行或下一列(行或列取决于滚动的方向)。每行都会根据情况,包含尽可能多的Cells。Cells可以是相同的尺寸,也可以是不同的尺寸。下面是FlowLayout的一些特性:

  • 面向线性布局
  • 可配置为网格
  • 一组lines
  • 具有页眉和页脚

3.2.2. 自定义 Flow Layout

  Flow Layout可以定制的主要功能如下:

  • Item size
  • Line spacing
  • Inter cell spacing
  • Scrolling direction
  • Header and forter size
  • Section inset

  几乎所有的定制属性都是在UICollectionViewFlowLayout中,delegate实际上是collection view 的delegate.UICollectionViewDelegateFlowLayout只是对UICollectionViewDelegate进行了一些扩展。

3.2.2.1. Item size(每个Item的大小)
3.2.2.1.1. 在全局中配置
@property(CGSize)itemSize

layout.itemSize= CGSizeMake(,);
3.2.2.1.2. 通过delegate对每个item进行配置
collectionView:layout:sizeForItemAtIndexPath:
{ return...; }
3.2.2.2. Line spacing(每行的间距)
3.2.2.2.1. 在全局中配置
@property(CGFloat) minimumLineSpacing
3.2.2.2.2. 通过delegate对每个Section进行配置
ollectionView:layout:minimumLineSpacingForSectionAtIndex:
3.2.2.3. Inter cell spacing(每行内部cell item的间距)
3.2.2.3.1. 在全局中配置
@property(CGFloat) minimumInteritemSpacing
3.2.2.3.2. 通过delegate对每个section进行配置
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
3.2.2.4. Scrolling direction(滚动方向)

  设置scrollDirection属性即可。两个值如下:UICollectionViewScrollDirectionVertical和UICollectionViewScrollDirectionHorizontal。即纵向或横向。

3.2.2.5. Header and footer size(页眉和页脚大小)

  页眉和页脚,即UICollectionElementKindSectionHeader和UICollectionElementKindSectionFooter,通过数据源方法提供内容:

-collectionView:viewForSupplementaryElementOfKind:atIndexPath:

  同样需要注册一个类,并从队列中取出view:

- registerClass:forSupplementaryViewOfKind:withReuseIdentifier:

-registerNib:forSupplementaryViewOfKind:withReuseIdentifier:

-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:

  页眉和页脚,当垂直的时候,需要设置的是Height,当水平的时候,需要设置的是Width。页眉和页脚的size配置方式如下:

3.2.2.5.1. 在全局中配置
@property(CGSize) headerReferenceSize

@property(CGSize) footerReferenceSize
3.2.2.5.2. 通过delegate对每个item进行配置
collectionView:layout:referenceSizeForHeaderInSection:

collectionView:layout:referenceSizeForFooterInSection:
3.2.2.6. Sections Insets

  通过两个图来看一下什么是Section Insets:

3.2.2.6.1. 在全局中配置
@propertyUIEdgeInsets sectionInset;
3.2.2.6.2. 通过delegate对每个item进行配置
- (UIEdgeInsets)collectionView:layout:insetForSectionAtIndex:

iOS 之UICollectionView 之原理介绍的更多相关文章

  1. iOS 之 UICollectionView

    1. iOS 之 UICollectionView 之 原理介绍 2. iOS 之 UICollectionView 之 开发步骤 之 OC 3. iOS 之 UICollectionView 之 开 ...

  2. 【如何快速的开发一个完整的iOS直播app】(原理篇)

    原文转自:袁峥Seemygo    感谢分享.自我学习 目录 [如何快速的开发一个完整的iOS直播app](原理篇) [如何快速的开发一个完整的iOS直播app](播放篇) [如何快速的开发一个完整的 ...

  3. iOS:app直播---原理篇

    [如何快速的开发一个完整的iOS直播app](原理篇) 转载自简书@袁峥Seemygo:http://www.jianshu.com/p/7b2f1df74420   一.个人见解(直播难与易) 直播 ...

  4. UIContainerView纯代码实现及原理介绍

    UIContainerView纯代码实现及原理介绍 1.1-在StoryBoard中使用UIContainerView 1.2-纯代码使用UIContainerView 1.3-UIContainer ...

  5. [iOS 开发] WebViewJavascriptBridge 从原理到实战 · Shannon's Blog

    前言:iOS 开发中,h5 和原生实现通信有多种方式, JSBridge 就是最常用的一种,各 JSBridge 类库的实现原理大同小异,这篇文章主要是针对当前使用最为广泛的 WebViewJavas ...

  6. 03 Yarn 原理介绍

    Yarn 原理介绍 大纲: Hadoop 架构介绍 YARN 产生的背景 YARN 基础架构及原理   Hadoop的1.X架构的介绍   在1.x中的NameNodes只可能有一个,虽然可以通过Se ...

  7. 04 MapReduce原理介绍

    大数据实战(上) # MapReduce原理介绍 大纲: * Mapreduce介绍 * MapReduce2运行原理 * shuffle及排序    定义 * Mapreduce 最早是由googl ...

  8. iOS app 程序启动原理

    iOS app 程序启动原理 Info.plist: 常见设置     建立一个工程后,会在Supporting files文件夹下看到一个"工程名-Info.plist"的文件, ...

  9. Android Animation学习(一) Property Animation原理介绍和API简介

    Android Animation学习(一) Property Animation介绍 Android Animation Android framework提供了两种动画系统: property a ...

随机推荐

  1. SSH整合中为获取表单对象Action类实现的接口及拦截器配置

    保存员工或者用户信息时,以员工为例,是通过表单收集信息的,需要把这些信息赋给一个对象,然后保存到数据库中.对应的Action类须实现Preparable接口及ModelDriven接口,且在Actio ...

  2. 内联函数 inline 漫谈

    内联函数存在的结论是: 引入内联函数是为了解决函数调用效率的问题 由于函数之间的调用,会从一个内存地址调到另外一个内存地址,当函数调用完毕之后还会返回原来函数执行的地址.函数调用会有一定的时间开销,引 ...

  3. 天棋哥哥大战AlphaGo

    天棋哥哥大战AlphaGo Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 20  Solved: 9 [Submit][Status][Web Boa ...

  4. LightOJ 1341 Aladdin and the Flying Carpet(整数拆分定理)

    分析:题目并不难理解,就是一些细节上的优化需要我们注意,我在没有优化前跑了2000多MS,优化了一些细节后就是400多MS了,之前还TLE了好几次. 方法:将整数拆分为质因子以后,表达为这样的形式,e ...

  5. 学习笔记——观察者模式Observer

    观察者模式,当事件发生时,调用相应观察者的方法进行“通知”.Subject中使用一个数据结构存储需要通知的观察者对象,执行Notify时,执行所有观察者的Update方法.

  6. HDU 2444 The Accomodation of Students

    首先是要构造二分图,然后二分图的最大匹配. 还有没完全证明过我的方法的正确性,但是AC了..... #include<cstdio> #include<cstring> #in ...

  7. Itext简绍及操作PDF文件

    iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...

  8. jq之简单的表单验证

    <body> <form method="post" action=""> <div class="int"& ...

  9. Hibernate中,将session绑定到线程时,在保存和查询数据的代码里,要正确的关闭session

    比如有个保存的方法 // 保存 public void save(){ Transaction t = XXX Session s = getSession.beginTransaction(); X ...

  10. STM8|STM32 看门狗使用

    源:STM8|STM32 看门狗使用 STM8和STM32都配备了独立看门狗,其作用之大不言而喻.以下为STM8及STM32的独立看门狗使用例: 对于STM32单片机: #define SYS_IWD ...