直接上代码:

  1. //
  2. // RootViewController.m
  3. //
  4. //
  5. #import "RootViewController.h"
  6. #import "CollectionViewCell.h"
  7. @interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
  8. @property (nonatomic, retain) UICollectionView *collectionView ; // 集合视图
  9. @end
  10. @implementation RootViewController
  11. - (void)dealloc {
  12. [_collectionView release];
  13. [super dealloc];
  14. }
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. // 1、在创建集合视图对象之前须要创建集合视图的布局类对象,用于对集合视图的单元格做布局。
  18. UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
  19. // 2、为相关属性
  20. // 设置最小行间距
  21. flowLayout.minimumLineSpacing = 5;
  22. // 设置最小列间距
  23. flowLayout.minimumInteritemSpacing = 5;
  24. // 设置 itemSize
  25. flowLayout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 40) / 4, 120);
  26. // 设置分区的内边距
  27. flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
  28. // 设置滑动方向,默认是纵向滑动。
  29. // flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  30. // 指定页眉大小
  31. flowLayout.headerReferenceSize = CGSizeMake(200, 60);
  32. // 指定页脚大小
  33. flowLayout.footerReferenceSize = CGSizeMake(200, 40);
  34. self.collectionView = [[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout] autorelease];
  35. // 须要制定代理对象
  36. self.collectionView.dataSource = self;
  37. self.collectionView.delegate = self;
  38. // 加入集合视图显示
  39. [self.view addSubview:self.collectionView];
  40. // 为集合视图注冊单元格类型
  41. [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
  42. // 为集合视图注冊页眉页脚的类型
  43. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
  44. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
  45. }
  46. // 为集合视图的每个分区指定 item 的数量
  47. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  48. return 100;
  49. }
  50. // 为集合视图的每个 Item 指定相应的单元格
  51. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  52. CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  53. cell.titleLable.text = [NSString stringWithFormat:@"(%ld, %ld)", indexPath.section, indexPath.item];
  54. // cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
  55. return cell;
  56. }
  57. // 集合视图的页眉页脚视图使用的是 UICollectionReusableView 或其子类的对象,同一时候页眉页脚通过 kind 来区分。而且使用专门的重用机制。来完毕对页眉页脚视图的管理。
  58. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  59. if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
  60. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  61. headerView.backgroundColor = [UIColor orangeColor];
  62. return headerView;
  63. }
  64. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  65. footerView.backgroundColor = [UIColor redColor];
  66. return footerView;
  67. }
  68. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  69. UIViewController *viewController = [[UIViewController alloc] init];
  70. [self.navigationController pushViewController:viewController animated:YES];
  71. [viewController release];
  72. }
  73. - (void)didReceiveMemoryWarning {
  74. [super didReceiveMemoryWarning];
  75. // Dispose of any resources that can be recreated.
  76. }
  77. /*
  78. #pragma mark - Navigation
  79. // In a storyboard-based application, you will often want to do a little preparation before navigation
  80. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  81. // Get the new view controller using [segue destinationViewController].
  82. // Pass the selected object to the new view controller.
  83. }
  84. */
  85. @end

  1. //
  2. // CollectionViewCell.h
  3. //
  4. //
  5. #import <UIKit/UIKit.h>
  6. @interface CollectionViewCell : UICollectionViewCell
  7. @property (nonatomic, retain) UILabel *titleLable ; //
  8. @end
  1. //
  2. // CollectionViewCell.m
  3. //
  4. //
  5. #import "CollectionViewCell.h"
  6. @implementation CollectionViewCell
  7. - (void)dealloc {
  8. [_titleLable release];
  9. [super dealloc];
  10. }
  11. - (UILabel *)titleLable {
  12. if (!_titleLable) {
  13. self.titleLable = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
  14. _titleLable.backgroundColor = [UIColor lightGrayColor];
  15. _titleLable.textColor = [UIColor whiteColor];
  16. _titleLable.font = [UIFont systemFontOfSize:16];
  17. _titleLable.textAlignment = NSTextAlignmentCenter;
  18. [self.contentView addSubview:_titleLable];
  19. }
  20. return _titleLable;
  21. }
  22. @end

UICollectionView 集合视图 的使用的更多相关文章

  1. UICollectionView集合视图的概念

    如何创建UICollectionView 集合视图的布局UICollectionViewFlowLayout 自定义cell 布局协议UICollectionViewDelegateFlowLayou ...

  2. UICollectionView 集合视图用法,自定义Cell

    在View里面 //1.创建UICollectionViewFlowLayout UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFl ...

  3. UICollectionView(集合视图)以及自定义集合视图

    一.UICollectionView集合视图           其继承自UIScrollView.         UICollectionView类是iOS6新引进的API,用于展示集合视图,布局 ...

  4. iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)

    两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点:   表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...

  5. 集合视图UICollectionView 介绍及其示例程序

    UICollectionView是一种新的数据展示方式,简单来说可以把它理解成多列的UITableView.如果你用过iBooks的话,可 能你还对书架布局有一定印象,一个虚拟书架上放着你下载和购买的 ...

  6. swift:创建集合视图UICollectionView

    swift中创建集合视图和OC中差不多,主要是实现UICollectionViewDataSource数据源协议和UICollectionViewDelegateFlowLayout自定义布局协议,其 ...

  7. 【推荐】iOS集合视图的可重新排序的layout

    在实际项目中你或许会遇到在一个集合视图中移动一项到另外一个位置,那么此时我们需要对视图中的元素进行重新排序,今天推荐一个很好用的第三方类LXReorderableCollectionViewFlowL ...

  8. 集合视图控制器(CollectionViewController) 、 标签控制器(TabBarController) 、 高级控件介绍

    1 创建集合视图,设置相关属性以满足要求 1.1 问题 集合视图控制器UIConllectionViewController是一个展示大量数据的控制器,系统默认管理着一个集合视图UICollectio ...

  9. iOS集合视图单元格高亮和选中的区别

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...

随机推荐

  1. [转载]-win7启动本地MongoDB的四种方式

    2016年04月07日 09:52:34 cherry__cheng 阅读数:19451 标签: win7启动本地MongoDB的四种方式快速启动本地mongodb 更多 个人分类: mongodb& ...

  2. 一个Web报表项目的性能分析和优化实践(三) :提高Web应用服务器Tomcat的内存配置,并确认配置正确

    摘要 上一篇,一个Web报表项目的性能分析和优化实践(一):小试牛刀,统一显示SQL语句执行时间 ,讲述了项目优化的整体背景,重点讲述了统一显示了Web项目SQL语句的执行时间. 本篇,将重点介绍提高 ...

  3. COGS——T2084. Asm.Def的基本算法

    http://cogs.pro/cogs/problem/problem.php?pid=2084 ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间 ...

  4. Java面向切面原理与实践

    Java面向切面原理与实践 一. 面向切面编程是什么 首先用一句话概括:面向切面编程(AOP)就是对某些具有相似点的代码进行增强. 相似点可以是同一个包.使用相同的注解.public的方法.以Impl ...

  5. OpenCv 人脸检測的学习

    近期公司要组织开发分享,可是自己还是新手真的不知道分享啥了,然后看了看前段时间研究过OpenCv,那么就分享他把. openCv就不介绍了,说下人脸检測.事实上是通过openCv里边已经训练好的xml ...

  6. redis之字符串命令源代码解析(二)

    形象化设计模式实战             HELLO!架构                     redis命令源代码解析 在redis之字符串命令源代码解析(一)中讲了get的简单实现,并没有对 ...

  7. vector和list容器之间的复制

    #include <iostream> #include <list> #include <string> #include <vector> #inc ...

  8. html 下载文件代码

    首先在html中加个a标签 <a class="menu" href="/cmdb/file_down" download="ljctest.t ...

  9. 简单的quartz 可视化监听管理界面

    spring-quartz. 导包.配置,不在此介绍. 简单的quartz管理界面,包括触发器的暂停.恢复.删除.修改(暂无),任务的运行.触发添加.创建,删除. 扩展内容:日志的管理,添加和创建触发 ...

  10. 2015上海网络赛 HDU 5478 Can you find it 数学

    HDU 5478 Can you find it 题意略. 思路:先求出n = 1 时候满足条件的(a,b), 最多只有20W对,然后对每一对进行循环节判断即可 #include <iostre ...