UICollectionView基本用法

创建:

/*

UICollectionViewFlowLayout

是系统提供的UICollectionViewLayout

用于直接进行简单的横向排版、竖直排版

*/

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc] init];

/*

scrollDirection滚动方向

UICollectionViewScrollDirectionHorizontal:横

UICollectionViewScrollDirectionVertical:竖

*/

layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

/*

UICollectionView

初始化必须给个UICollectionViewLayout

其使用方法特别类似UITableView

也有俩个代理delegate、dataSource

*/

UICollectionView *collectionView = [[UICollectionViewalloc] initWithFrame:self.view.boundscollectionViewLayout:layout];

collectionView.delegate = self;

collectionView.dataSource = self;

/*

collectionview使用注册特别的简单

注册之后根本不需要考虑为空怎么办

注册时系统自动判断是否有空余的没有就创建

其中注册时可以使用xib或者纯代码

以下分别注册单元格、头、尾

其中注册头、尾时使用的一个方法所有需要声明你注册的哪个

UICollectionElementKindSectionHeader:头

UICollectionElementKindSectionFooter:尾

*/

[collectionView registerClass:[CollectionViewCellclass] forCellWithReuseIdentifier:@"cell"];

[collectionView registerClass:[ReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"];

[collectionView registerClass:[ReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"footer"];

[self.viewaddSubview:collectionView];

#pragma mark - UICollectionViewDataSource

//返回几组

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

return  5;

}

//返回几个

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

{

return21;

}

//返回单元格

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

{

/*

注册,注册之前,前面必须注册过

注册时直接输入IndexPath就有了

我这边直接自定义单元格

*/

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];

switch (indexPath.section) {

case0:

cell.backgroundColor = [UIColorredColor];

break;

case1:

cell.backgroundColor = [UIColoryellowColor];

break;

case2:

cell.backgroundColor = [UIColorblueColor];

break;

case3:

cell.backgroundColor = [UIColorpurpleColor];

break;

case4:

cell.backgroundColor = [UIColorgreenColor];

break;

default:

break;

}

cell.titleLabel.text = [NSStringstringWithFormat:@"%d -- %d",indexPath.section,indexPath.row];

return cell;

}

//返回头尾

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

{

//声明头尾

ReusableView *reusable = nil;

//判断当前需要头还是尾

if (kind == UICollectionElementKindSectionHeader) {

//注册头

reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"forIndexPath:indexPath];

reusable.backgroundColor = [UIColorwhiteColor];

reusable.titleLabel.text = [NSStringstringWithFormat: @"头:%d",indexPath.section];

}else{

//注册尾

reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"footer"forIndexPath:indexPath];

reusable.backgroundColor = [UIColorgrayColor];

reusable.titleLabel.text = [NSStringstringWithFormat: @"尾:%d",indexPath.section];

}

return reusable;

}

#pragma mark - UICollectionViewDelegate

#pragma mark - UICollectionViewDelegateFlowLayout

//返回单元格宽高

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

{

float width = 150.0f;

float height = 100.0f;

returnCGSizeMake(width, height);

}

//返回单元格上左下右

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

{

UIEdgeInsets edge = UIEdgeInsetsMake(10, 5, 10, 5);

return edge;

}

//返回头宽高

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

float width = 150.0f;

float height = 100.0f;

returnCGSizeMake(width, height);

}

//返回尾宽高

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

float width = 100.0f;

float height = 100.0f;

returnCGSizeMake(width, height);

}

时间: 2024-12-06 10:25:10

UICollectionView基本用法的相关文章

UICollectionView基础用法

#import "ViewController.h" #define kScreenSize [UIScreen mainScreen].bounds.size //遵守协议 @interface ViewController () <UICollectionViewDataSource,UICollectionViewDelegate> {     UICollectionView *_collectionView;      } @property (nonatomic

UICollectionView

UICollectionView是Apple在iOS6开始,提供给我们的一个强大的控件.最简单的UICollectionView就是GridView,标准的UICollectionView包含如下几个部分: 1 UICollectionReusableView<SupplementaryView>:可以指定section的Header或者Footer 2 UICollectionViewCell:用于展示内容的主体,对于不同的cell可以指定不同尺寸和不同的内容 再次说明,UICollecti

UICollectionView基本使用详解(OC)

概述 UICollectionView是从iOS6开始引入使用的,目前应用非常广泛,很牛逼!老外的博客也是这么说的(传送门) ## 与UITableView的初步比较 UITableView应该是大家最熟悉的控件了,UICollectionView的使用与之类似,但又有所区别,如下介绍.相同点: 1.都是通过datasource和delegate驱动的(datasource和delegate官方文档传送),因此在使用的时候必须实现数据源与代理协议方法; 2.性能上都实现了循环利用的优化. 不同点

一周随笔--15.11.02

一周新知识点记录(15.11.02) 一.storyboard中搭建tableViewCell 在storyboard中搭建tableView,一种是以UITableViewController为容器,另一种则是以UIViewController为容器,拖出一个tableView来. 当以UITableViewController为容器时,UITableViewCell可以是动态也可以是静态的,若是静态的则控制器可以不关联文件.(具体待验证)http://m.blog.csdn.net/blog

Swift 写一个简单界面(实战-新手)

原文链接 在这篇博文中你可以看到那些内容呢, 首先这是一个用tableView纯代码Swift写的简单界面, 你可以看到下面这些 - 使用Alamofire 进行网络请求 - 使用MJExtension 进行字典转模型 - 使用HanekeSwift 进行图片的赋值 - 如何写一个模型(M) - 如何自定义一个UITableViewCell Alamofire 简单网络请求 func XTNetworkReq(url: String){ print("SUMMER_TEST_1") A

iOS开发之窥探UICollectionViewController(五) --一款炫酷的图片浏览组件

本篇博客应该算的上CollectionView的高级应用了,从iOS开发之窥探UICollectionViewController(一)到今天的(五),可谓是由浅入深的窥探了一下UICollectionView的用法,这些用法不仅包括SDK中自带的流式布局(UICollectionViewDelegateFlowLayout)而且介绍了如何根据你的需求去自定义属于你自己的CollectionView.自定义的CollectionView可谓是非常灵活,其灵活性也决定了其功能的强大.Collect

多任务卡片动画实现原理

实现效果 控件 - UICollectionView 这个动画是用 UICollectionView 实现的,简单讲下 UICollectionView 的工作原理.这里用到的 UICollectionView 也就3部分:ViewController(简称VC).UICollectionViewCell.UICollectionViewLayout. ViewController :在VC里,UICollectionView 的用法跟 UITableView 的用法类似.这里的初始化方法与 U

UICollectionView在Swift3.0中的用法

UICollectionView在Swift3.0中的用法 UICollectionView的初始化跟OC中是相似的,创建 GameView 集成自 UICollectionView .注意不同于UITableView的用法,他需要用 UICollectionViewFlowLayout 来指定一些需要设置的属性,或者可以通过遵守 UICollectionViewDelegateFlowLayout 这个代理来实现.下面我用设置属性的方式来实现的,比较方便. //布局 layout.scroll

IOS中UICollectionView的基本用法

本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类. 使用UICollectionView 必须实现UICollectionViewDataSource,UIColle