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. (Nhibernate )XML配置文件的智能感知(IntelliSense)功能

      XML配置文件的智能感知(IntelliSense)功能 在使用一些第三方的程序库(Nhibernate,Ibatis...)常常需要手工编写比较复杂的配置文件,如果没有像VS IDE那样的Int ...

  2. iOS多线程(转)

    关于iOS多线程,你看我就够了 字数8596 阅读28558 评论74 喜欢313 在这篇文章中,我将为你整理一下 iOS 开发中几种多线程方案,以及其使用方法和注意事项.当然也会给出几种多线程的案例 ...

  3. JQuery IN ACTION读书笔记之一: JQuery选择器

    本章关注两个通过$()使用的常用功能: 通过选择器选择DOM元素,创建新DOM元素. 2.1 选择操作元素 JQuery采用了CSS的语法,而CSS的语法你可能已经很熟悉了.当然,JQuery也做了扩 ...

  4. Swift 开源 Linux Ubuntu Install

    Swift 开源了,它现在变成跨平台的了,开源后的 Swift 不止能运行在 MAC 和 iOS 平台,现在也可以运行在 Linux 平台了.swift.org 网站上面提供了在 Linux 上面安装 ...

  5. Java进阶01 String类(转载)

    String类包含在java.lang包中.这个包会在Java启动的时候自动import,所以可以当做一个内置类(built-in class).我们不需要显式的使用import引入String类. ...

  6. AndroidHttpClient和HttpEntity详解

    AndroidHttpClient结构: public final class AndroidHttpClient extends Object implements HttpClient 前言:这类 ...

  7. 转载:给bash的提示符设置不同的颜色 一个很常用的功能,效果如下:

    原文来自:http://www.cnblogs.com/cyttina/archive/2013/01/08/2850406.html 一个很常用的功能,效果如下: 这样就可以很轻易的将输入的指令和其 ...

  8. 九度OJ 1358:陈博的平均主义 (遍历、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:354 解决:191 题目描述: 在JOBDU团队里,陈博是最讲平均主义的人了,但并不是像梁山好汉那样能够做到有钱同花,有肉同吃,毕竟,他还是 ...

  9. C# MD5加密与校验 引用

    using System; using System.Security.Cryptography; using System.Text; class Example { // Hash an inpu ...

  10. 安装和配置jenkins

    1.首先准备java环境,安装JDK 2.下载jenkins至Linux服务器 sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkin ...