直接上代码:

//
// RootViewController.m
//
// #import "RootViewController.h"
#import "CollectionViewCell.h" @interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate> @property (nonatomic, retain) UICollectionView *collectionView ; // 集合视图 @end @implementation RootViewController - (void)dealloc {
[_collectionView release];
[super dealloc];
} - (void)viewDidLoad {
[super viewDidLoad]; // 1、在创建集合视图对象之前须要创建集合视图的布局类对象,用于对集合视图的单元格做布局。 UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
// 2、为相关属性
// 设置最小行间距
flowLayout.minimumLineSpacing = 5;
// 设置最小列间距
flowLayout.minimumInteritemSpacing = 5;
// 设置 itemSize
flowLayout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 40) / 4, 120);
// 设置分区的内边距
flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
// 设置滑动方向,默认是纵向滑动。 // flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// 指定页眉大小
flowLayout.headerReferenceSize = CGSizeMake(200, 60);
// 指定页脚大小
flowLayout.footerReferenceSize = CGSizeMake(200, 40); self.collectionView = [[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout] autorelease]; // 须要制定代理对象
self.collectionView.dataSource = self;
self.collectionView.delegate = self; // 加入集合视图显示
[self.view addSubview:self.collectionView]; // 为集合视图注冊单元格类型
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
// 为集合视图注冊页眉页脚的类型
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"]; } // 为集合视图的每个分区指定 item 的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 100;
} // 为集合视图的每个 Item 指定相应的单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.titleLable.text = [NSString stringWithFormat:@"(%ld, %ld)", indexPath.section, indexPath.item];
// cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
return cell;
} // 集合视图的页眉页脚视图使用的是 UICollectionReusableView 或其子类的对象,同一时候页眉页脚通过 kind 来区分。而且使用专门的重用机制。来完毕对页眉页脚视图的管理。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor orangeColor];
return headerView;
}
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
footerView.backgroundColor = [UIColor redColor];
return footerView;
} - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *viewController = [[UIViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

//
// CollectionViewCell.h
//
// #import <UIKit/UIKit.h> @interface CollectionViewCell : UICollectionViewCell @property (nonatomic, retain) UILabel *titleLable ; // @end
//
// CollectionViewCell.m
//
// #import "CollectionViewCell.h" @implementation CollectionViewCell - (void)dealloc {
[_titleLable release];
[super dealloc];
} - (UILabel *)titleLable {
if (!_titleLable) {
self.titleLable = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
_titleLable.backgroundColor = [UIColor lightGrayColor];
_titleLable.textColor = [UIColor whiteColor];
_titleLable.font = [UIFont systemFontOfSize:16];
_titleLable.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:_titleLable];
}
return _titleLable;
} @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. windows下laravel5安装

    第一步:安装composer    网上教程非常多,自行百度 第二步:使用composer create-project laravel/laravel learnlaravel5 5.0.22   ...

  2. F - Humidex(1.4.2)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

  3. JAVA爬虫Nutch、WebCollector的正则约束

    爬虫爬取时,须要约束爬取的范围. 基本全部的爬虫都是通过正則表達式来完毕这个约束. 最简单的,正则: http://www.xinhuanet.com/.* 代表"http://www.xi ...

  4. Unity UGUI——UI基础,Canvas

    主题:画布--Canvas 内容:创建Canvas UI控件的绘制顺序 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTXJfQUhhbw==/font/5 ...

  5. python list的+,+=,append,extend

    面试题之中的一个. def func1(p): p = p + [1] def func2(p): p += [1] p1 = [1,2,3] p2 = [1,2,3] func1(p1) func2 ...

  6. 遇到 Form 性能问题怎么办 performance issue

    性能问题是比較复杂的问题. 一般由performance team 负责, 可是常见的情况是, 我们 INV team 定义的 view 不好, 导致查询性能较差. 这个必须由产品组和 perform ...

  7. Git的日常处理流程

    前提 本地有2个分支,一个是master,还有一个是local master 默认追踪origin/master local 通过git branch -u origin/master来映射 开发的时 ...

  8. Delegates, Events, and Anonymous Methods 委托、事件与匿名方法

    http://www.cnblogs.com/r01cn/archive/2012/11/30/2795977.html

  9. 简单缓存Cache

    接口 interface ICache { /// <summary> /// 添加 /// </summary> /// <param name="key&q ...

  10. 通过视频展示如何通过Samba配置PDC

    通过视频展示如何通过Samba配置PDC(Linux企业应用案例精解补充视频内容) 本文通过视频,真实地再现了在Linux平台下如何通过配置smb.conf文件而实现Samba Server模拟win ...