UICollectionView 具体解说学习
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 具体解说学习的更多相关文章
- UITableView和UICollectionView的方法学习一
参考资料 UITableView UICollectionView UICollectionViewDataSource UICollectionViewDelegate UICollectionVi ...
- 《转》循环神经网络(RNN, Recurrent Neural Networks)学习笔记:基础理论
转自 http://blog.csdn.net/xingzhedai/article/details/53144126 更多参考:http://blog.csdn.net/mafeiyu80/arti ...
- (原)UICollectionView的基本使用
UICollectionView基本使用 学习iOS一段时间了,早听说了UICollectionView的强大一直没有机会使用,今天自己研究了一下. UICollectonView类似UITableV ...
- 循环神经网络RNN
转自 http://blog.csdn.net/xingzhedai/article/details/53144126 更多参考:http://blog.csdn.net/mafeiyu80/arti ...
- swift系统学习控件篇:UITableView+UICollectionView
工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UITableView: // // ViewController.swift // UIt ...
- UICollectionLayout布局 —— UIKit之学习UICollectionView记录二《流水布局》
重点知识 一. 加载collectionView注意事项 1.创建collectionView,有两种方式 :一种是xib和一种是纯代码:设置代理和数据源,注册cell,配置流水布局的属性,如上.下. ...
- iOS 学习 - 20 UICollectionView 移动 Item ,类似背包
有100个 item,数据源只有20个,只能在 20 个之间移动,防止 item 复用,出现 bug 方法一:苹果自带 //UICollectionViewDataSource- (BOOL)coll ...
- 自定义UICollectionLayout布局 —— UIKit之学习UICollectionView记录一《瀑布流》
一.思路 思路一:比较每一行所有列的cell的高度,从上到下(也就是从第一行开始),从最短的开始计算,(记录下b的高度和索引,从开始计算,依次类推) 思路二:设置上.下.左.右间距和行间距.列间距及列 ...
- 对UICollectionView的学习
UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...
随机推荐
- [LOJ] 分块九题 2
https://loj.ac/problem/6278 区间修改,查询区间第k大. 块内有序(另存),块内二分. 还是用vector吧,数组拷贝排序,下标搞不来.. //Stay foolish,st ...
- <Spring Data JPA>一 JPA原生
1.pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- 条款12:复制对象时勿忘其每一个成分(Copy all parts of an object)
NOTE: 1.Copying 函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 2.不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个 ...
- Python处理PDF-通过关键词定位-截取PDF中的图表
起因: 因为个人原因, 这些天了解了一下Python处理PDF的方法. 首先是PDF转txt, 这个方法比较多, 这里就不再赘述, 主要聊一下PDF中的图片获取. 这里用我自己的例子, 不过具体情况还 ...
- hihocoder 1584 Bounce (数学 && 规律) ACM-ICPC北京赛区2017网络赛
题意: 给定一副n*m的格子图, 问从左上角的点开始往右下角滑,碰到墙壁就反弹, 碰到角落就停止, 问恰好经过一次的格子有多少个. 如图,恰好经过一次的格子有39个. 分析: 首先要引入两个概念, “ ...
- mysql 常用命令(一)
1. 函数向日期添加指定的时间间隔 DATE_ADD(date,INTERVAL expr type)eg:DATE_ADD(CURDATE(),INTERVAL 1 MONTH) //在当前时间加一 ...
- Leetcode 230.二叉搜索树第k小的数
二叉搜索树第k小的数 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输 ...
- left join 与left outer join的区别
joinn 语句有三种:inner join, left outer join 和 right outer join都可以简写,分别为join,left join,right join.
- 【思维+贪心】codeforces Game of the Rows
http://codeforces.com/contest/839/problem/B [题意] 给定n组人,告诉每组人的人数,这些人要在飞机上坐座位 飞机上座位的分布看图可以知道,12 3456 ...
- 安卓巴士Android开发神贴整理
10个经典的Android开源应用项目 http://www.apkbus.com/android-13519-1-1.html 安卓巴士总结了近百个Android优秀开源项目,覆盖Android开发 ...