本章通过先总体介绍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

iOS开发- UICollectionView详解+实例相关推荐

  1. iOS开发设计模式详解

    在软件开发中一般认为有23种设计模式(design pattern),这是软件开发中的较高的境界了.在iOS开发中最常用的有哪些设计模式呢?我们今天来分析一下: 一.[代理模式] 使用场景:当一个类的 ...

  2. iOS开发-Runtime详解(简书)

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

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

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

  4. 苹果iOS开发系列--详解Swift 3.0语言的重大变化

    概述 从写第一篇Swift文章的时候到现在Swift已经从1.2发展到了今天的3.0,这期间由于Swift目前还在发展阶段并不能向下兼容,因此第一篇文章中的部分代码在当前的Xcode环境中已经无法运行 ...

  5. iOS开发CAAnimation详解

    Core Animation,即为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能.Core Anima ...

  6. iOS开发之详解连连支付集成

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

  7. iOS开发——Handoff详解

    iOS8推出一个新特性,叫做Handoff.Handoff中文含义为换手(把接力棒传给下一个人),可以在一台Mac和iOS设备上开始工作,中途将工作交换到另一个Mac或iOS设备中进行.这个在iOS8 ...

  8. iOS 7: iPhone/iPad应用开发技术详解

    iOS 7: iPhone/iPad应用开发技术详解 作者:刘一道 出版社:机械工业出版社 出版年:2013-11 页数:507 定价:79.00元 ISBN:9787111440512 样章下载:h ...

  9. iOS 7 iPhone iPad应用开发技术详解

    2019独角兽企业重金招聘Python工程师标准>>> iOS 7 iPhone iPad应用开发技术详解 本书内容全面,从 Objective-C语法知识.iOS功能特 性,到高级 ...

最新文章

  1. UnixLinux大学教程 读书笔记【1-3章】
  2. 操作系统学习1:操作系统概述
  3. 背包问题(多重背包+0-1背包)
  4. OpenFeign 全方位讲解
  5. K8S集群搭建:安装kubeadm集群部署工具
  6. win10共享打印机怎么设置_怎么设置打印机共享?
  7. 支持markdown的服务器,基于tornado实现的一个markdown解析服务器
  8. drool 7.x 属性 : agenda-group
  9. exploring spring
  10. 老李分享:《Linux Shell脚本攻略》 要点(二)
  11. 欢迎大家推荐使用小技巧
  12. 使用原生JS封装Ajax
  13. python获取灰度图边界
  14. matlab函数多个零点,MATLAB中求一个双变量函数的零点
  15. php上证指数抽奖代码,上证指数etf基金代码,场内etf基金一览表
  16. 苹果亮度自动调节怎么关闭_笔记本黑苹果系统亮度调节驱动
  17. 【PPT】跨境电商的N个知识点普及
  18. Increasing trend of scientists to switch between topics论文翻译
  19. c语言*p1 什么意思,p1什么意思_p1,意思_词汇大全意思全集
  20. Mac如何更改系统默认的播放器?

热门文章

  1. oracle性能优化总结
  2. 洛谷P4016 负载平衡问题
  3. java day10(续day9)
  4. tomcat架构分析(valve机制)
  5. 关于textarea包在div的自适应问题
  6. HDU 2722 Here We Go(relians) Again (spfa)
  7. [VB] Option Explicit
  8. python怎么开发安卓程序_怎样用python开发安卓app-到底如何使用Python开发Android程序.txt...
  9. 神经网络训练集的图片到底是如何相互作用的?
  10. 杭州电子科技大学计算机组成原理期末试卷,杭州电子科技大学计算机组成原理期末样卷(A)...