UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。

使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。

1.首先我们创建CollectionView(代码如下)

- (void)initCollectionView

{

// 我是用masnory进行屏幕适配的 这个坐标你也可以直接给出。

[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(self.view.mas_bottom).offset(0);

make.top.offset(0); //顶部间隔

make.left.offset(0);//左边

make.bottom.offset(-10);//底部

make.right.offset(0);//右边

//

}];

self.collectionView.backgroundColor = [UIColor colorWithRed:241 green:241 blue:241 alpha:1];//背景色

self.collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);//插入内容的位置 与边缘

self.collectionView.delegate = self;代理协议

self.collectionView.dataSource = self;数据源协议

[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

创建新的cell

}

2.创建collectionview 必须要添加 UICollectionViewFlowLayout

//使用懒加载的方法

- (UICollectionView *)collectionView

{

if (!_collectionView)

{

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

flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 50)/4, (SCREEN_WIDTH - 50)/4 + 20);

flowLayout.minimumLineSpacing = 10; 每排的间隔

flowLayout.minimumInteritemSpacing = 10; 每行的间隔

self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];

[self.view addSubview:self.collectionView];

}

return _collectionView;

}

3.接下来 我们需要新建一个类(CollectionviewCell)

cell.h 文件中呢  我们把需要展示的内容 都有属性展示出来

比如 展示一个图片 和文字

@property (nonatomic, strong) UIImageView *familyImageView;

@property (nonatomic, strong) UILabel *titleLabel;

所以  接下来在.m中写这2个控件(我的坐标都是masnory适配  也可以直接给出)

-(instancetype)initWithFrame:(CGRect)frame

{

if (self = [super initWithFrame:frame])

{

self.familyImageView=[[UIImageView alloc] init];

self.titleLabel = [[UILabel alloc] init];

self.titleLabel.font = [UIFont systemFontOfSize:13];

self.titleLabel.textAlignment=NSTextAlignmentCenter;

[self.contentView addSubview:self.titleLabel];

[self.contentView addSubview:self.familyImageView];

//make  是masnory的一个属性 用于定义坐标位置

[self.familyImageView mas_makeConstraints:^(MASConstraintMaker *make)

{

make.top.offset = 10;

make.left.offset = 10;

make.bottom.equalTo(self.titleLabel.mas_top).offset(-10);

make.right.offset = -10;

}];

[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make)

{

make.left.offset = 0;

make.bottom.offset = 0;

make.right.offset = 0;

make.height.offset = 20;

}];

}

return self;

}

4.前提工作都基本做完了  接下来 就是实现协议代理的 方法 用来展示内容 显示在屏幕中

//区数

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

return 1;

}

//每个区显示的小模块

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

return 18;

}

cell 上面展示的内容

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

{

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

注意上面这个CollectionViewCell 是你新建的cell  名称是一样的

下面就是展示你需要展示的内容了 直接用cell 打点调用就可以用属性了。

cell.familyImageView.image = _arr[indexPath.row];这是我之前定义的一个图片

cell.titleLabel.text=nameArr[indexPath.row];label

return cell;

}

cell的点击方法

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

{

}

大致就是这些了,愿猿友们早日迎娶白富美,走向人生peak!

CollectionView就是这么简单!的更多相关文章

  1. 第19月第2天 cellForItemAtIndexPath 返回空

    1. 动画需要获取当前的 Cell ,下面的调用在 viewDidLoad 中,有时候返回 nil,有时候成功. UICollectionView *cell = (UICollectionView* ...

  2. iOS-UICollectionView快速构造/拖拽重排/轮播实现

    代码地址如下:http://www.demodashi.com/demo/11366.html 目录 UICollectionView的定义 UICollectionView快速构建GridView网 ...

  3. Swift 使用CollectionView 实现图片轮播封装就是这样简单

    前言: 这篇你可以学会自定义视图,创建collectionView,协议的使用,定时器; 自制图片 先上Demo:Github上封装好的下载即用, 好用请Star Thanks首先新建一个继承于UIV ...

  4. 用collectionview实现瀑布流-转(后面附demo,供参考)

    算法总体思路 先说一下总体上的思路.既然图片的大小.位置各不一样,我们很自然地会想到需要算出每个item的frame,然后把这些frame赋值给当前item的UICollectionViewLayou ...

  5. CollectionView水平和竖直瀑布流的实现

    最近在项目中需要实现一个水平的瀑布流(即每个Cell的高度是固定的,但是长度是不固定的),因为需要重写系统 UICollectionViewLayout中的一些方法通过计算去实现手动布局,所以本着代码 ...

  6. IOS自定义日历控件的简单实现(附思想及过程)

    因为程序要求要插入一个日历控件,该空间的要求是从当天开始及以后的六个月内的日历,上网查资料基本上都说只要获取两个条件(当月第一天周几和本月一共有多少天)就可以实现一个简单的日历,剩下的靠自己的简单逻辑 ...

  7. collectionView布局原理及瀑布流布局方式

    一直以来都想研究瀑布流的具体实现方法(起因是因为一则男女程序员应聘的笑话,做程序的朋友应该都知道).最近学习到了瀑布流的实现方法,瀑布流的实现方式有多种,这里应用collectionView来重写其U ...

  8. 用collectionview实现瀑布流-转

    算法总体思路 先说一下总体上的思路.既然图片的大小.位置各不一样,我们很自然地会想到需要算出每个item的frame,然后把这些frame赋值给当前item的UICollectionViewLayou ...

  9. iOS-UICollectionView的简单使用(原创)

    前言 UICollectionView是一种新的数据展示方式,简单来说可以把他理解成多列的UITableView(请一定注意这是UICollectionView的最最简单的形式).如果你用过iBook ...

随机推荐

  1. HDU 5968 异或密码 【模拟】 2016年中国大学生程序设计竞赛(合肥)

    异或密码 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Des ...

  2. python 正则表达式 贪婪模式的简介和匹配时的几种模式

    看到一篇文章,关于python正则的,http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 贪婪模式与非贪婪模式: 正则表达式通常用于 ...

  3. FZU 2213 Common Tangents 第六届福建省赛

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2213 题目大意:两个圆,并且知道两个圆的圆心和半径,求这两个圆共同的切线有多少条,若有无数条,输出-1,其他条 ...

  4. UVA 4728 Squares(凸包+旋转卡壳)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...

  5. [SAM4N学习笔记]LED点灯程序

    一.准备工作:      将上一节搭建的工程模板复制一份,命名为"1.blink",这作为我们开发的第一个程序. 二.程序编写:      板子上只有一个可控制的LED,就是LED ...

  6. Objective-C categories in static library

    ASK: Can you guide me how to properly link static library to iphone project. I use staic library pro ...

  7. div border-radius

    可以画个1/4之一的圆也可以画整个圆 <html> <style type="text/css"> div{ background-color: #000; ...

  8. MongoDB:The Definitive Guide CHAPTER 2 Getting Started

    MongoDB is very powerful, but it is still easy to get started with. In this chapter we’ll introduce ...

  9. Hibernate查询之Example查询

    org.hibernate.criterion.Example 类允许你通过一个给定实例构建一个条件查询. 此实例的属性值将做成查询条件. Cat cat = new Cat(); cat.setSe ...

  10. 《linux程序设计》--读书笔记--第十四章信号量、共享内存和消息队列

    信号量:用于管理对资源的访问: 共享内存:用于在程序之间高效的共享数据: 消息队列:在程序之间传递数据的一种简单方法: 一.信号量 临界代码:需要确保只有一个进程或者一个执行线程可以进入这个临界代码并 ...