UICollectionView初探

collectionView的使用跟tableview差不多,比table更强大(ios6.0以后)

1、需实现的协议<UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

2、标识cell和header、footer

static NSString* cellIdentifier = @"identifier";

static NSString* cellSmallIdentifier = @"smallidentifier";

static NSString* cellHeaderIdentifier = @"headeridentifier";

static NSString* cellFooterIdentifier = @"footeridentifier";

3、初始化

UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];

m_collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];

[self.view addSubview:m_collectionView];

m_collectionView.delegate = self;

m_collectionView.dataSource = self;

[m_collectionView registerClass:[BigCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];

[m_collectionView registerClass:[SmallCollectionViewCell class] forCellWithReuseIdentifier:cellSmallIdentifier];//两种类型的cell

[m_collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:cellHeaderIdentifier];//section header

[m_collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:cellFooterIdentifier];//section footer

4、定义cell类

.m

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

_aLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];

_aLable.textColor = [UIColor blackColor];

_aLable.font = [UIFont systemFontOfSize:15.0];

[self addSubview:_aLable];

_bLable = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 45)];

_bLable.textColor = [UIColor blackColor];

_bLable.font = [UIFont systemFontOfSize:15.0];

[self addSubview:_aLable];

}

return self;

}

5、定义header、footer

.m

- (instancetype)initWithFrame:(CGRect)frame{//不能用init

self = [super initWithFrame:frame];

if (self != nil) {

_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375, 45)];//不能直接写frame

_label.backgroundColor = [UIColor purpleColor];

_label.textColor = [UIColor whiteColor];

_label.font = [UIFont systemFontOfSize:15.0];

[self addSubview:_label];

}

return self;

}

6、collectionView协议

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

return 5;

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 5;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

if (indexPath.row > 1) {

SmallCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellSmallIdentifier forIndexPath:indexPath];

cell.contentView.backgroundColor = [UIColor grayColor];

cell.aLable.text = [NSString stringWithFormat:@"第几个啊 %ld", indexPath.row];

return cell;

}

BigCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

cell.contentView.backgroundColor = [UIColor yellowColor];

cell.aLable.text = [NSString stringWithFormat:@"第几个 %ld", indexPath.row];

cell.bLable.text = [NSString stringWithFormat:@"b %ld", indexPath.row];

return cell;

}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

NSString *reuseIdentifier;

if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){

reuseIdentifier = cellFooterIdentifier;

}else{

reuseIdentifier = cellHeaderIdentifier;

}

MyCollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier  forIndexPath:indexPath];

if ([kind isEqualToString:UICollectionElementKindSectionHeader]){

view.label.text = [NSString stringWithFormat:@"这是header:%ld",indexPath.section];

}

else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){

view.backgroundColor = [UIColor lightGrayColor];

view.label.text = [NSString stringWithFormat:@"这是footer:%ld",indexPath.section];

}

return view;

}

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

{

NSLog(@"%ld,%ld点击", indexPath.section, indexPath.row);

}

#pragma mark 尺寸

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

NSInteger row = indexPath.row;

switch (row) {

case 0:

case 1:

return CGSizeMake(335/2.0, 45+45*indexPath.section);

break;

default:

return CGSizeMake(325/3.0, 45);

break;

}

}

//行间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{

return 10;

}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

return UIEdgeInsetsMake(15, 15, 5, 15);//分别为上、左、下、右

}

//返回头headerView的大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

CGSize size={320,45};

return size;

}

//返回头footerView的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section

{

CGSize size={320,45};

return size;

}

效果图:

可以自定义layout实现瀑布流,layout一些delegate方法可以在初始化的时候直接设置好

时间: 2024-12-15 23:45:44

UICollectionView初探的相关文章

进阶之初探nodeJS

一.前言 在"初探nodeJS"随笔中,我们对于node有了一个大致地了解,并在最后也通过一个示例,了解了如何快速地开启一个简单的服务器. 今儿,再次看了该篇随笔,发现该随笔理论知识稍多,适合初级入门node,固萌生一个想法--想在该篇随笔中,通过一步步编写一个稍大一点的node示例,让我们在整体上更加全面地了解node. so,该篇随笔是建立在"初探nodeJS"之上的,固取名为"进阶之初探nodeJS". 好了,侃了这多,那么我们即将实现一个

从273二手车的M站点初探js模块化编程

前言 这几天在看273M站点时被他们的页面交互方式所吸引,他们的首页是采用三次加载+分页的方式.也就说分为大分页和小分页两种交互.大分页就是通过分页按钮来操作,小分页是通过下拉(向下滑动)时异步加载数据. 273这个M站点是产品推荐我看的.第一眼看这个产品时我就再想他们这个三次加载和翻页按钮的方式,那么小分页的pageIndex是怎么计算的.所以就顺便看了下源码. 提到看源码时用到了Chrome浏览器的格式化工具(还是朋友推荐我的,不过这个格式化按钮的确不明显,不会的话自行百度). 三次加载和分

[转载]HDFS初探之旅

转载自 http://www.cnblogs.com/xia520pi/archive/2012/05/28/2520813.html , 感谢虾皮工作室这一系列精彩的文章. Hadoop集群(第8期)_HDFS初探之旅 1.HDFS简介 HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据存储管理的基础,是基于流数据模式访问和处理超大文件的需求而开发的,可以运行于廉价的商用服务器上.它所具有的高容错.高可靠性.高可扩展性.高

UICollectionView介绍使用

UICollectionView是一种类似于UITableView但又比UITableView功能更强大.更灵活的视图,这是源于它将UICollectionView对cell的布局交给了UICollectionViewLayout,而且允许用户自定义layout来进行布局. 下面是UICollectionView合并内容和布局并生成最终界面的一个流程: 当UICollectionView显示内容时,先从Data source(数据源)获取cell,然后交给UICollectionView.再从U

MongoDB初探系列之二:认识MongoDB提供的一些常用工具

在初探一中,我们已经可以顺利的将MongoDB在我们自己的机器上跑起来了.但是在其bin目录下面还有一些我们不熟知的工具.接下来,将介绍一下各个小工具的用途以及初探一中MongoDB在data文件夹下创建的文件的用途. 1.bin目录下面的各种小工具简介及使用方式 bsondump.exe 用于将导出的BSON文件格式转换为JSON格式mongo.exe mongoDB的客户端 mongod.exe 用于启动mongoDB的Server mongodump.exe 用于从mongodb数据库中导

Asynchronous Pluggable Protocols 初探

Asynchronous Pluggable Protocols,异步可插入协议,允许开发者创建可插协议处理器,MIME过滤器,以及命名空间处理器工作在微软IE4.0浏览器以及更高版本或者URL moniker中.这涉及到Urlmon.dll动态链接库所公开(输出)的可插协议诸多功能,本文不进行深入的原理讲解,只对它其中之一的应用进行解析,那就是如何将一个应用程序注册为URL协议. 应用场景: tencent协议: 当我们打开"tencent://message/?uin=要链接的QQ号 &qu

重新认识HTML,CSS,Javascript 之node-webkit 初探

今天我们来系统的.全面的 了解一下前端的一些技术,将有助于我们写出 更优秀的 产品 出来. 什么是HTML? HTML 是用来描述网页的一种语言. HTML 包含一些根节点,子节点,文本节点,属性节点,组成, 它通过一系列预定义标签来描述网页结构,如: <title>This is title</title> ,这个表明该网页的标题是 This is title. 什么是CSS? CSS 指层叠样式表 (Cascading Style Sheets),它描述浏览器显示如何显示htm

UIScrollView的delegate方法妙用之让UICollectionView滑动到某个你想要的位置

一个UICollectionView有好多个cell,滑动一下,谁也不知道会停留在哪个cell,滑的快一点,就会多滑一段距离,反之则会滑的比较近,这正是UIScrollview用户体验好的地方. 如果想要UICollectionView停留到某个cell的位置,可以用 - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPos

java进阶06 线程初探

线程,程序和进程是经常容易混淆的概念. 程序:就是有序严谨的指令集 进程:是一个程序及其数据在处理机上顺序执行时所发生的活动 线程:程序中不同的执行路径,就是程序中多种处理或者方法. 线程有两种方法实现 一:继承Thread 覆盖run方法 package Thread; public class Thread1 { public static void main(String[] args){ MyThread1 thread1=new MyThread1(); thread1.setName