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的更多相关文章

  1. ios 中的UI控件学习总结(1)

    UIKit框架提供了非常多功能强大又易用的UI控件 下面列举一些在开发中可能用得上的UI控件 UIButton 按钮 UILabel 文本标签 UITextField 文本输入框 UIImageVie ...

  2. UI控件概述

    常见UI控件 UIKit框架提供了非常多功能强大又易用的UI控件,以便于开发者打造出各式各样的App 以下列举一些在开发中常见的UI控件(稍后补上图片示例) 1.UILabel– 文本标签:作用是显示 ...

  3. ANDROID L——Material Design详解(UI控件)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...

  4. WinForm/Silverlight多线程编程中如何更新UI控件的值

    单线程的winfom程序中,设置一个控件的值是很easy的事情,直接 this.TextBox1.value = "Hello World!";就搞定了,但是如果在一个新线程中这么 ...

  5. 富客户端 wpf, Winform 多线程更新UI控件

    前言 在富客户端的app中,如果在主线程中运行一些长时间的任务,那么应用程序的UI就不能正常相应.因为主线程要负责消息循环,相应鼠标等事件还有展现UI. 因此我们可以开启一个线程来格外处理需要长时间的 ...

  6. UI控件(复习一下)

    如何修改控件状态• 可见,确实需要经常修改控件状态• 那如何去修改控件的状态呢?方法很简单➢ 每一个UI控件都是一个对象➢ 修改UI控件的状态,其实就是修改控件对象的属性➢ 比如修改UILabel显示 ...

  7. IOS学习资源收集--开发UI控件相关

    收集的一些本人了解过的iOS开发UI控件相关的代码资源(本文持续补充更新) 内容大纲: 1.本人在github上也上传了我分装好的一些可重复利用的UI控件 2.计时相关的自定义UILabel控件 正文 ...

  8. 《深入理解Windows Phone 8.1 UI控件编程》基于最新的Runtime框架

    <深入理解Windows Phone 8.1 UI控件编程>本书基于最新的Windows Phone 8.1 Runtime SDK编写,全面深入地论述了最酷的UI编程技术:实现复杂炫酷的 ...

  9. AppleWatch___学习笔记(二)UI布局和UI控件

    1.UI布局 直接开发,你会发现Apple Watch并不支持AutoLayout,WatchKit里有个类叫做WKInterfaceGroup,乍一看像是UIView,但是这货其实是用来布局的.从 ...

随机推荐

  1. 【LeetCode】 Rotate List 循环链表

    题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...

  2. 创建Spring Boot项目的几种方式总结

    一.我们可以使用Spring Initializr来创建SpringBoot项目. Spring Initializr从本质上来说就是一个Web应用程序,它能为你生成Spring Boot项目结构.虽 ...

  3. selenium 自动化工具

    问题 今天在使用selenium+PhantomJS动态抓取网页时,出现如下报错信息: UserWarning: Selenium support for PhantomJS has been dep ...

  4. LOCAL_SHARED_LIBRARIES 与 LOCAL_LDLIBS,LOCAL_LDFLAGS的区别

    LOCAL_LDLIBS :链接的库不产生依赖关系,一般用于不需要重新编译的库,如库不存在,则会报错找不到.且貌似只能链接那些存在于系统目录下本模块需要连接的库.如果某一个库既有动态库又有静态库,那么 ...

  5. python django -6 常用的第三方包或工具

    常用的第三方包或工具 富文本编辑器 缓存 全文检索 celery 布署 富文本编辑器 借助富文本编辑器,管理员能够编辑出来一个包含html的页面,从而页面的显示效果,可以由管理员定义,而不用完全依赖于 ...

  6. MongoDB API和python操作

    安装 下载mongodb的版本,两点注意 根据业界规则,偶数为稳定版,如1.6.X,奇数为开发版,如1.7.X 32bit的mongodb最大只能存放2G的数据,64bit就没有限制 到官网,选择合适 ...

  7. .Net 单例模式(Singleton)

    每台计算机可以有若干个打印机,但只能有一个Printer Spooler, 以避免两个打印作业同时输出到打印机中.每台计算机可以有若干传真卡,但是只应该有一个软件负责管理传真卡,以避免出现两份传真作业 ...

  8. 傅里叶叠层成像FP(Fourier Ptychographic Imaging)查资料

    傅里叶叠层成像FP(Fourier Ptychographic Imaging) 傅里叶叠层显微术(FPM)是一种新型的计算显微成像技术,FPM与传统显微术照明方式不同,常采用可编程LED阵列进行不同 ...

  9. OpenCV学习笔记十一:opencv_ocl模块

    一,简介: 基于OpenCL优化的代码.

  10. 高通Quick Charge高速充电原理分析

    1 QC 2.0 1.1 高通Quick Charge 2.0 高速充电原理分析 高通的QC2.0高速充电须要手机端和充电器都要支持才行. 当将充电器端通过数据线连到手机上时,充电器默认的是将D+和D ...