UICollectionView 和UITableView非常像,是APPLE公司在iOS 6后推出的用于处理图片这类UITableView 布局困难的控件,和UITableView 一样,它也有自己的Datasource和delegate。以下具体说下像这种方式的效果.

首先来看看UICollectionView 的DataSource。

@protocol UICollectionViewDataSource <NSObject>
@required - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section; // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; @optional - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView; // The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath; @end

能够看到和UITableView 一样,它有两个必须实现的方法:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section//有多少个item

-dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;//每一个长什么样,它要使用dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:来生成

其他的两个是分别有几部分cell,和UITableView中的numberOfSection一样.默认是1个,viewForSupplementaryElementOfKind这个则是用来做出表头和表尾的。

UICollectionViewDelegate 它的代理方法全是可选的,常常且主要用到的就是:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

这是表明选中后要做什么.

以下来实现一下:

首先在自定义的头文件里加入一个UIColletionView的属性(比直接用实例变量好,具体请看Effective objective-c的学习笔记1)配置各种属性,然后加到self.view上。

@property (nonatomic, strong) ZJCollectionViewFlowOut *collectionViewFlowLayout;
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:self.collectionViewFlowLayout];
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
[self.view addSubview:_collectionView];

再设置viewController 遵守delegate和dataSource;

@interface ZJCollectionViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@end

这样再xcode 上新的一行输入- collection就会出现非常多collectionView的提示了.

实现:collectionView:cellForItemAtIndexPath:

由于这种方法要用到前面说的那个注冊的cell所以先建一个UICollectionViewCell,顺便搞定那个FlowLayout.

Cell的内容当然能够自定义的了.

在viewDidLoad中创建CollectionView的以下加上:

    [_collectionView registerClass:[ZJCollectionViewCell class] forCellWithReuseIdentifier:kCellReuseIdentifier];
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ZJCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseIdentifier forIndexPath:indexPath];
NSString *image = @"201502192144014806.jpg";
cell.userImageView.image = [UIImage imageNamed:image];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.pictureArray.count;
}

在viewDidLoad中collectionView创建之前要创建那个布局的

collectionViewFlowLayout 对象.

    self.collectionViewFlowLayout = [[ZJCollectionViewFlowOut alloc]init];

当中在collectionViewFlowLayout里面是这种:

- (id)init
{
if (self = [super init]) {
self.minimumInteritemSpacing = 1.0;//item 之间的行的距离
self.minimumLineSpacing = 0.0;//item 之间竖的距离
self.itemSize = (CGSize){[UIScreen mainScreen].bounds.size.width/3,[UIScreen mainScreen].bounds.size.width/3};
// self.sectionInset = UIEdgeInsetsMake(4, 4, 4, 4); 这个是设置一个section的距离上下上左下右间距。 }
return self;
}

得出的结果例如以下图:



这是一个简单的UICollectionView的展示,时间不多。有空再具体点

UICollectionView 具体解说学习的更多相关文章

  1. UITableView和UICollectionView的方法学习一

    参考资料 UITableView UICollectionView UICollectionViewDataSource UICollectionViewDelegate UICollectionVi ...

  2. 《转》循环神经网络(RNN, Recurrent Neural Networks)学习笔记:基础理论

    转自 http://blog.csdn.net/xingzhedai/article/details/53144126 更多参考:http://blog.csdn.net/mafeiyu80/arti ...

  3. (原)UICollectionView的基本使用

    UICollectionView基本使用 学习iOS一段时间了,早听说了UICollectionView的强大一直没有机会使用,今天自己研究了一下. UICollectonView类似UITableV ...

  4. 循环神经网络RNN

    转自 http://blog.csdn.net/xingzhedai/article/details/53144126 更多参考:http://blog.csdn.net/mafeiyu80/arti ...

  5. swift系统学习控件篇:UITableView+UICollectionView

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UITableView: // // ViewController.swift // UIt ...

  6. UICollectionLayout布局 —— UIKit之学习UICollectionView记录二《流水布局》

    重点知识 一. 加载collectionView注意事项 1.创建collectionView,有两种方式 :一种是xib和一种是纯代码:设置代理和数据源,注册cell,配置流水布局的属性,如上.下. ...

  7. iOS 学习 - 20 UICollectionView 移动 Item ,类似背包

    有100个 item,数据源只有20个,只能在 20 个之间移动,防止 item 复用,出现 bug 方法一:苹果自带 //UICollectionViewDataSource- (BOOL)coll ...

  8. 自定义UICollectionLayout布局 —— UIKit之学习UICollectionView记录一《瀑布流》

    一.思路 思路一:比较每一行所有列的cell的高度,从上到下(也就是从第一行开始),从最短的开始计算,(记录下b的高度和索引,从开始计算,依次类推) 思路二:设置上.下.左.右间距和行间距.列间距及列 ...

  9. 对UICollectionView的学习

    UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...

随机推荐

  1. SCOI2013 数数

    题目描述 题解: 很玄学的一道数位$dp$,看了很多篇题解才懂. 直接挂$l$的题解. 代码: #include<cstdio> #include<cstring> #incl ...

  2. InnoDB INFORMATION_SCHEMA Buffer Pool Tables

    InnoDB INFORMATION_SCHEMA Buffer Pool Tables InnoDB INFORMATION_SCHEMA缓冲池表提供有关InnoDB缓冲池中页面的缓冲池状态信息和元 ...

  3. 条款6:若不想使用编译器自动生成的函数,就该明确拒绝(Explicity disallow the use of compiler-generated functions you do not want)

    class uncopyable{ protected: uncopyable(){};                                                         ...

  4. POJ 3169 Layout(差分约束 线性差分约束)

    题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...

  5. python接口自动化-token参数关联登录(登录拉勾网)

    前言 登录网站的时候,经常会遇到传token参数,token关联并不难,难的是找出服务器第一次返回token的值所在的位置,取出来后就可以动态关联了 登录拉勾网 1.先找到登录首页https://pa ...

  6. Android ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape

     Android ShapeDrawable之OvalShape.RectShape.PaintDrawable.ArcShape Android图形图像基础之OvalShape.RectShap ...

  7. [luoguP1773] 符文之语_NOI导刊2010提高(02)(DP)

    传送门 f[i][j]表示前i个数余数为j的最优解 sum[i][j]表示字符串i~j所构成的数 #include <cstdio> #include <cstring> #d ...

  8. Mysql的常见几种错误:1045,1044

    Mysql的常见几种错误: 一.在进入 mysql 数据库时出错   # mysql -u root -p Enter password: ERROR 1045 (28000): Access den ...

  9. 飞行路线(BZOJ 2763)

    题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...

  10. BZOJ1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘

    一傻逼题调了两天.. n<=30 * m<=30的地图,0表示可以放平台,1表示本来有平台,2表示不能走,3起点4终点,走路方式为象棋的日字,求:从起点走到终点,至少要放多少平台,以及放平 ...