本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView。

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

使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。

下面先给出常用到的一些方法。(只给出常用的,其他的可以查看相关API)

  1. #pragma mark -- UICollectionViewDataSource
  1. //定义展示的UICollectionViewCell的个数
  2. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  3. {
  4. return 30;
  5. }
  1. //定义展示的Section的个数
  2. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  3. {
  4. return 1;
  5. }
  1. //每个UICollectionView展示的内容
  2. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. static NSString * CellIdentifier = @"GradientCell";
  5. UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  6. cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:1.0f];
  7. return cell;
  8. }
  1. #pragma mark --UICollectionViewDelegateFlowLayout
  1. //定义每个UICollectionView 的大小
  2. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. return CGSizeMake(96, 100);
  5. }
  1. //定义每个UICollectionView 的 margin
  2. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  3. {
  4. return UIEdgeInsetsMake(5, 5, 5, 5);
  5. }
  1. #pragma mark --UICollectionViewDelegate
  1. //UICollectionView被选中时调用的方法
  2. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  5. cell.backgroundColor = [UIColor whiteColor];
  6. }
  1. //返回这个UICollectionView是否可以被选择
  2. -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. return YES;
  5. }

下面通过一个例子具体介绍下。(例子来自网络。但是是通过第三方获得的,无法取得链接。还望见谅。)

iOS CollectionView的出现是一大福利,再也不用用TableView来定义复杂的多栏表格了,用法与Table类似,只是Cell必须自己添加,无默认模式

由于CollectionView没有默认的Cell布局,所以一般还是自定义方便又快捷

一、自定义Cell

1、新建类CollectionCell继承自UICollectionViewCell

2、新建Xib,命名为CollectionCell.xib

a.选中CollectionCell.xib删掉默认的View,从控件中拖一个Collection View Cell(图3)到画布中,设置大小为95*116;

b.选中刚刚添加的Cell,更改类名为CollectionCell,如图4

c.在CollectionCell.xib的CollectionCell中添加一个ImageView和一个Label(图5)

d.创建映射, 图6,图7

e.选中CollectionCell.m , 重写init方法

  1. - (id)initWithFrame:(CGRect)frame
  2. {
  3. self = [super initWithFrame:frame];
  4. if (self)
  5. {
  6. // 初始化时加载collectionCell.xib文件
  7. NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil];
  8. // 如果路径不存在,return nil
  9. if (arrayOfViews.count < 1)
  10. {
  11. return nil;
  12. }
  13. // 如果xib中view不属于UICollectionViewCell类,return nil
  14. if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])
  15. {
  16. return nil;
  17. }
  18. // 加载nib
  19. self = [arrayOfViews objectAtIndex:0];
  20. }
  21. return self;
  22. }

f.选中CollectionCell.xib 修改其identifier为CollectionCell。

二、定义UICollectionView;

1、拖动一个Collection View到指定ViewController的View上

2、连线dataSource和delegate,并创建映射,命名为CollectionView

3、选中CollectionView的标尺,将Cell Size的Width和Height改成与自定义的Cell一样的95*116,图8

4、选中CollectionView的属性,可以修改其属性,比如是垂直滑动,还是水平滑动,选择Vertical或Horizontal

5、选中CollectionViewCell,修改Class,继承自CollectionCell

5、在ViewDidLoad方法中声明Cell的类,在ViewDidLoad方法中添加,此句不声明,将无法加载,程序崩溃

其中,CollectionCell是这个Cell的标识(之前几步已经定义过了。 )

  1. [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];

6、在ViewController.h中声明代理

  1. @interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

7、在.m文件中实现代理方法

  1. //每个section的item个数
  2. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  3. {
  4. return 12;
  5. }
  6. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  7. {
  8. CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
  9. //图片名称
  10. NSString *imageToLoad = [NSString stringWithFormat:@"%d.png", indexPath.row];
  11. //加载图片
  12. cell.imageView.image = [UIImage imageNamed:imageToLoad];
  13. //设置label文字
  14. cell.label.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.row,(long)indexPath.section];
  15. return cell;
  16. }

8 。效果如图10

点击某项后跳转事件与UITableView类似,实现代理方法

  1. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

即可,不赘述

转自   http://www.cnblogs.com/ios8/p/iOS-UICollectionView.html

 

[转] iOS开发- UICollectionView详解+实例的更多相关文章

  1. iOS开发- UICollectionView详解+实例

    本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewC ...

  2. iOS开发——UI篇OC篇&UICollectionView详解+实例

    UICollectionView详解+实例 实现步骤: 一.新建两个类 1.继承自UIScrollView的子类,比如HMWaterflowView * 瀑布流显示控件,用来显示所有的瀑布流数据 2. ...

  3. iOS开发:详解Objective-C runTime

    Objective-C总Runtime的那点事儿(一)消息机制 最近在找工作,Objective-C中的Runtime是经常被问到的一个问题,几乎是面试大公司必问的一个问题.当然还有一些其他问题也几乎 ...

  4. iOS开发-Runtime详解

    iOS开发-Runtime详解 简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [recei ...

  5. iOS开发——Block详解

    iOS开发--Block详解 1. Block是什么 代码块 匿名函数 闭包--能够读取其他函数内部变量的函数 函数变量 实现基于指针和函数指针 实现回调的机制 Block是一个非常有特色的语法,它可 ...

  6. iOS开发——MVC详解&Swift+OC

    MVC 设计模式 这两天认真研究了一下MVC设计模式,在iOS开发中这个算是重点中的重点了,如果对MVC模式不理解或者说不会用,那么你iOS肯定学不好,或者写不出好的东西,当然本人目前也在学习中,不过 ...

  7. IOS开发之----详解在IOS后台执行

    文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后台运行一小段时间. 还有三种类型的可以运行在后以,1.音乐2.location 3.voip 文二 ...

  8. iOS开发--Bison详解连连支付集成简书

    "最近由于公司项目需要集成连连支付,文档写的不是很清楚,遇到了一些坑,因此记录一下,希望能帮到有需要的人." 前面简单的集成没有遇到什么坑,在此整理一下官方的集成文档,具体步骤如下 ...

  9. iOS开发之详解正则表达式

    本文由Charles翻自raywenderlich原文:NSRegularExpression Tutorial: Getting Started更新提示:本教程被James Frost更新到了iOS ...

随机推荐

  1. 2B The least round way

    题目大意: 一个n*n的矩阵,从矩阵的左上角开始,每次移动到下面或者右面,移动到右下角结束. 要求走的路径上的所有数字乘起来,乘积得到的值后面的0最少.   #include <iostream ...

  2. Codeforces Round #260 (Div. 1) --B. A Lot of Games (Trie)

    B. A Lot of Games Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings f ...

  3. ASIHttpRequest或者SDWebImage给UIImageView加载图片的逻辑是什么样子的

    非常非常喜欢SDWebImage,就算后来AF也做了图片缓存,我也还是一直使用.图片缓存真的是一件令人头疼的事,但是SDWebImage封装了一切,你所需要的只是简单的set一个URL:1.设置一个展 ...

  4. Fluent-EDEM耦合计算颗粒流动

    虽然说Fluent提供了很多方法用于处理颗粒在流体中的运动行为,然而这些方法都有其各自的适用性.DPM适用于稀薄颗粒的情况,欧拉模型.Mixture模型及DDPM模型虽然可以考虑稠密颗粒相,但并不能考 ...

  5. [连载]JavaScript讲义(03)--- JavaScript面向对象编程

  6. Android 图片选择器

    图片选择器,遍历系统所有图片并显示,点击查看大图,长按选中,并将结果返回 字体颜色res/color建立text_selecor.xml <selector xmlns:android=&quo ...

  7. cocos2d-x项目过程记录(cocos2d-x的新知)

    1.给CCMenuItem带上点击参数(这是CCNode的一个属性) CCMenuItem *item = CCMenuItemSprite::create(unselectedPic, select ...

  8. [转] 多线程下变量-gcc原子操作 __sync_fetch_and_add等

    http://blog.sina.com.cn/s/blog_6f5b220601013zw3.html 非常好的原子操作,不用加锁:__sync_fetch_and_add GCC 提供的原子操作 ...

  9. [转] linux之sed用法

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed ...

  10. Android内存等信息

    1. Linux中proc目录下文件详解 http://wenku.baidu.com/view/2ce89f00a6c30c2259019ef1.html 2. Android系统/proc目录详解 ...