UICollectionView 集合视图 的使用
直接上代码:
//
// 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 集合视图 的使用的更多相关文章
- UICollectionView集合视图的概念
如何创建UICollectionView 集合视图的布局UICollectionViewFlowLayout 自定义cell 布局协议UICollectionViewDelegateFlowLayou ...
- UICollectionView 集合视图用法,自定义Cell
在View里面 //1.创建UICollectionViewFlowLayout UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFl ...
- UICollectionView(集合视图)以及自定义集合视图
一.UICollectionView集合视图 其继承自UIScrollView. UICollectionView类是iOS6新引进的API,用于展示集合视图,布局 ...
- iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)
两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点: 表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...
- 集合视图UICollectionView 介绍及其示例程序
UICollectionView是一种新的数据展示方式,简单来说可以把它理解成多列的UITableView.如果你用过iBooks的话,可 能你还对书架布局有一定印象,一个虚拟书架上放着你下载和购买的 ...
- swift:创建集合视图UICollectionView
swift中创建集合视图和OC中差不多,主要是实现UICollectionViewDataSource数据源协议和UICollectionViewDelegateFlowLayout自定义布局协议,其 ...
- 【推荐】iOS集合视图的可重新排序的layout
在实际项目中你或许会遇到在一个集合视图中移动一项到另外一个位置,那么此时我们需要对视图中的元素进行重新排序,今天推荐一个很好用的第三方类LXReorderableCollectionViewFlowL ...
- 集合视图控制器(CollectionViewController) 、 标签控制器(TabBarController) 、 高级控件介绍
1 创建集合视图,设置相关属性以满足要求 1.1 问题 集合视图控制器UIConllectionViewController是一个展示大量数据的控制器,系统默认管理着一个集合视图UICollectio ...
- iOS集合视图单元格高亮和选中的区别
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...
随机推荐
- python-排序算法 冒泡和快速排序
交换排序 交换排序有冒泡排序和快速排序 冒泡排序 冒泡排序就是每次找出最大(最小)元素,放在集合最前或最后,这是最简单的排序算法 print("未排序之前:",collection ...
- spring的PropertyPlaceholderConfigurer不生效的问题
经常出现这种问题,每次都debug知道原因,但每次都会忘记,所以记录一下. 原因:maven项目中使用了非maven管理的jar包(通过systemPath引用),这些jar包没有放在${projec ...
- ArcGIS api for javascript——显示一个信息窗口
描述 这个示例展示了在用户单击地图时如何在InfoWindow中显示信息.信息窗口是一个dijit (Dojo widget).信息窗口能够包含文本,字符,图片和任何通过HTML表示的事物.这个例子在 ...
- 各大IT企业招聘所须要求技能
1.中兴 ZTE 软件研发project师 工作地点:西安.深圳.上海.天津 主要职责: 1.从事通讯产品相关软件开发 2.进行软件具体设计,代码编写.单元測试.集成測试.系统測试等 3.进行软件代码 ...
- Java中发送http的get、post请求
近期做项目中,须要把消息通过中间件的形式通过http请求的方式推送给第三方,因此用到了http协议,小编花费了一个多小时.对于http协议中的post和get请求,封装了一个工具类.以下与大家分享一下 ...
- 【JavaScript】分秒倒计时器
一.基本目标 在JavaScript设计一个分秒倒计时器,一旦时间完毕使button变成不可点击状态 详细效果例如以下图.为了说明问题.调成每50毫秒也就是每0.05跳一次表, 真正使用的时候,把wi ...
- 78.Nodejs基础中间件Connect
转自:https://www.cnblogs.com/chris-oil/p/5625437.html 前言 “中间件”在软件领域是一个非常广的概念,除操作系统的软件都可以称为中间件,比如,消息中间件 ...
- 3.菜鸟教你一步一步开发 web service 之 axis 服务端创建
转自:https://blog.csdn.net/shfqbluestone/article/details/37610601 第一步,新建一个工程,如图: 选 Java 写一个工程名,选择好工程路径 ...
- 解决Maven项目相互依赖/循环依赖/双向依赖的问题
转自:https://blog.csdn.net/leolu007/article/details/53079875 添加新随笔很多时候随着项目的膨胀,模块会越来越多 ...
- BZOJ 1012 单调队列+二分
思路: 维护一个单减的序列 序号是单增的 每回二分查找第一个比询问的大的值 我手懒 用得lower_bound //By SiriusRen #include <cstdio> #incl ...