UI控件之UICollectionView
UICollectionView:集合视图,是iOS6.0后出现的,与UITableView类似,优势在于可以灵活的布局cell
UICollectionViewLayout:布局类,抽象类,一般定义子类,iOS提供了一个具体的子类UICollectionViewFlowLayout,可以实现网格布局
UICollectionViewFlowLayout *flow=[[UICollectionViewFlowLayout alloc]init];
设置布局的滚动方式(垂直滚动)
flow.scrollDirection=UICollectionViewScrollDirectionVertical;
设置item之间的最小间隔(实际间隔由item的大小和UIEdgeInsets决定的)
flow.minimumInteritemSpacing=10;
设置每行之间的最小间隔
flow.minimumLineSpacing=20;
初始化对象时需指定布局对象
_collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, 375, 603) collectionViewLayout:flow];
设置代理
_collectionView.dataSource=self;
_collectionView.delegate=self;
提前注册可重用单元格,@"MyCell":xib文件名,@"cell":xib文件中cell的identifier
[_collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
提前注册每组的headerView,footerView (可重用的)
[_collectionView registerClass:[CustomView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"supple"];
[_collectionView registerClass:[CustomView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"supple"];
设置系统不能自动调节布局
self.automaticallyAdjustsScrollViewInsets=NO;
设置背景色,默认是黑色
_collectionView.backgroundColor=[UIColor whiteColor];
UICollectionViewDataSouce的协议方法
设置item的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
显示每项的方法
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier=@"cell";
//从可重用队列中取出可重用的单元格项,如果没有,会自动按照之前注册过的样式创建
MyCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.headerImageView.image=[UIImage imageNamed:@"0.png"];
cell.titleLabel.text=@"test";
return cell;
}
设置分组数,默认是1
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
UICollectionViewDelegateFlowLayout的协议方法
设置每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
设置collectionView距上、左、下、右边侧的距离
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
设置每组的headerView的大小,如果垂直滚动,宽度和collectionView宽度一样,需要设置高度,不实现的话默认是0
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
设置每组的footerView的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
设置头尾视图
-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//可重用视图都是UICollectionReusableView的子类, 用来设置组的headerView(FooterView)
CustomView *view;
//显示每组的HeaderView
if([kind isEqualToString:UICollectionElementKindSectionHeader]){
//从可重用队列中取出view,已经提前注册过
view=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"supple" forIndexPath:indexPath];
view.backgroundColor=[UIColor yellowColor];
}//显示每组的footerView
else if([kind isEqualToString:UICollectionElementKindSectionFooter]){
view=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"supple" forIndexPath:indexPath];
view.backgroundColor=[UIColor blueColor];
}
return view;
}
UICollectionViewDelegateFlowLayout遵守了UICollectionViewDelegate协议,所以只要遵守UICollectionViewDelegateFlowLayout就可以访问两个协议的方法
UICollectionViewDelegate的协议方法
选中某项时执行的协议方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//获取用户选择的项的编号
NSLog(@"%ld",indexPath.item);
}
UI控件之UICollectionView的更多相关文章
- ios 中的UI控件学习总结(1)
UIKit框架提供了非常多功能强大又易用的UI控件 下面列举一些在开发中可能用得上的UI控件 UIButton 按钮 UILabel 文本标签 UITextField 文本输入框 UIImageVie ...
- UI控件概述
常见UI控件 UIKit框架提供了非常多功能强大又易用的UI控件,以便于开发者打造出各式各样的App 以下列举一些在开发中常见的UI控件(稍后补上图片示例) 1.UILabel– 文本标签:作用是显示 ...
- ANDROID L——Material Design详解(UI控件)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...
- WinForm/Silverlight多线程编程中如何更新UI控件的值
单线程的winfom程序中,设置一个控件的值是很easy的事情,直接 this.TextBox1.value = "Hello World!";就搞定了,但是如果在一个新线程中这么 ...
- 富客户端 wpf, Winform 多线程更新UI控件
前言 在富客户端的app中,如果在主线程中运行一些长时间的任务,那么应用程序的UI就不能正常相应.因为主线程要负责消息循环,相应鼠标等事件还有展现UI. 因此我们可以开启一个线程来格外处理需要长时间的 ...
- UI控件(复习一下)
如何修改控件状态• 可见,确实需要经常修改控件状态• 那如何去修改控件的状态呢?方法很简单➢ 每一个UI控件都是一个对象➢ 修改UI控件的状态,其实就是修改控件对象的属性➢ 比如修改UILabel显示 ...
- IOS学习资源收集--开发UI控件相关
收集的一些本人了解过的iOS开发UI控件相关的代码资源(本文持续补充更新) 内容大纲: 1.本人在github上也上传了我分装好的一些可重复利用的UI控件 2.计时相关的自定义UILabel控件 正文 ...
- 《深入理解Windows Phone 8.1 UI控件编程》基于最新的Runtime框架
<深入理解Windows Phone 8.1 UI控件编程>本书基于最新的Windows Phone 8.1 Runtime SDK编写,全面深入地论述了最酷的UI编程技术:实现复杂炫酷的 ...
- AppleWatch___学习笔记(二)UI布局和UI控件
1.UI布局 直接开发,你会发现Apple Watch并不支持AutoLayout,WatchKit里有个类叫做WKInterfaceGroup,乍一看像是UIView,但是这货其实是用来布局的.从 ...
随机推荐
- 高分辨率转HTML成PDF(ephtmltopdf.dll)
今天看到园友分享的 wkhtmtopdf--高分辨率转HTML成PDF(三) 想起我做 一键保存网页为PDF_V1.2 的时候,也研究过一段时间这个,一开始也看的是wkhtmltopdf,不过发现wk ...
- openwrt安装编译
官网安装编译推荐: https://wiki.openwrt.org/doc/howto/buildroot.exigence https://wiki.openwrt.org/doc/howto/b ...
- 【Mac + Python3.6 + ATX基于facebook-wda】之IOS自动化(二):安装facebook-wda库并编写简易自动化测试脚本
上一篇介绍完如何安装WDA,接下来开始正式安装开发库并编写自动化脚本. 目录: 一.安装facebook-wda库 二.通过WEditor定位元素 三.附录:学习资料 一.安装facebook-wda ...
- PHP递归实现无限级分类
在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性.那么PHP是如何实现无限级分类的呢?我们在本文中使用递归算法并结合mysql数据表实现无限级分类. 在一些复杂的系统中,要求对信 ...
- python 装饰器 (多个参数的函数,带参数的装饰器)
最简单的模板是这样的 #-*-coding:utf-8-*- def outer(func): def inner(): print 'before' func() print 'after' # r ...
- Mysql闪回技术之 binlog2sql
1.下载 https://github.com/danfengcao/binlog2sql http://rpmfind.net Search: python-pip pip 是一个Python包管理 ...
- DHCP动态主机配置协议
1.DHCP简述 某组织一旦获得了一个地址,它就可以为本组织内的主机与路由器接口逐个分配IP地址.系统管理通常可以手工配置路由器中的IP地址(静态分配).但这项任务目前通常更多是使用动态主机配置协议( ...
- 【BZOJ4403】序列统计 Lucas定理
[BZOJ4403]序列统计 Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取模的结果. Input 输入第 ...
- echarts+thinkphp 学习写的第一个程序
一.前台 建个名为map.html,代码如下. <!doctype html><html lang="en"><head> <meta c ...
- pycharm中格式标准化代码
点击之后,可以使代码标准化