UIConllectionView和UITableView类似,也是展示数据,但不同于UITableView那种规则的布局,UICollectionView可以实现不规则的布局,即瀑布流。

创建UICollectionView

UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:layout];

集合视图的创建,必须要指定布局,如果没有布局,显示不了任何东西,即layout。

 //创建一个布局对象,采用系统布局类UICollectinviewFlowLayout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

因为是系统的布局类,所以也是规则的,但可以自定义FlowLayout的,可以根据自己的需求,来创建不规则的网格。

可以对各个的布局细节分别进行设置

//设置最小的行间距
layout.minimumLineSpacing = ;
//设置item与item之间的间距
layout.minimumInteritemSpacing = ; //集合视图的分区间隔 //四个值 上左下右
layout.sectionInset = UIEdgeInsetsMake(, , , ); //设置集合视图的滑动方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical;// 向下
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 向右
CGFloat totalWidth = self.view.frame.size.width;
//设置每一个item的尺寸大小
// layout.itemSize = CGSizeMake((totalWidth - 40) / 3, 80);

当然,签订协议之后也可以通过方法进行设置

说到协议 ,协议 分为两个部分,数据源协议UICollectionViewDelegateSource和代理协议UICollectionViewDelegate

因为涉及到布局,也会签订的布局协议UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake((kWidth - ) / , );
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(, , , );
} - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return ;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return ;
}

UICollectionViewDataSource和UITableView一样,也有两个必须要实现的方法

//显示个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return ;
}
//每个cell显示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithRed:kColor green:kColor blue:kColor alpha:1.0];
cell.numberLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
return cell;
}

我这边cell的显示内容是显示一个Label。自定义cell,来设置label的格式。

同UITableView一样,每个item都可以点击,触发 方法

//item点击之后触发的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"分区数%ld, 行数%ld",indexPath.section, indexPath.row);
}

值得注意的是,集合视图不像表视图那样,集合视图,如果想显示内容,就必须注册cell

//集合视图如果想要显示内容,就必须将cell进行注册
[collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];

注意:

集合视图的不规则布局在日常使用还是比较频繁的,因为每个空间布局都不一定会是规则,也会有差别,通过自定义FlowLayout..来显示不同的布局

UICollectionView布局功能的更多相关文章

  1. iOS 8自动调整UITableView和UICollectionView布局

    本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...

  2. [转]iOS8 自动调整UITableView和UICollectionView布局

    转自:http://www.cocoachina.com/industry/20140825/9450.html (via:玉令天下的Blog)   本文讲述了UITableView.UICollec ...

  3. 利用修改div的位置+js对象存储div信息 实现简单的div自定义布局功能

    原文:利用修改div的位置+js对象存储div信息 实现简单的div自定义布局功能 利用修改div的位置+js对象存储div信息 实现简单的div自定义布局功能1.在界面上添加几个checkbox和一 ...

  4. ABBYY FineReader 15新增编辑页面布局功能

    ABBYY FineReader 15(Windows系统) 新增编辑页面布局功能,允许用户修改PDF数字文档的页面布局,包括添加或者删除文字段落,文字块以及图片,更改段落,文字块,图片位置.添加或者 ...

  5. UICollectionView布局cell的三种方式

    UICollectionViewFlowLayout里面: // 方法一 - (void)prepareLayout{} // 方法二 - (nullable NSArray<__kindof ...

  6. iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流

    上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...

  7. iOS下的界面布局利器-MyLayout布局框架

      Swift:TangramKit: https://github.com/youngsoft/TangramKit OC:MyLayout: https://github.com/youngsof ...

  8. iOS UICollectionView(转三)

    上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...

  9. iOS电商常见动画与布局、微信悬浮窗、音乐播放器、歌词解析、拖动视图等源码

    iOS精选源码 MXScroll 介绍 混合使用UIScrollView ios 电商demo(实现各种常见动画效果和页面布局) 一行代码集成微信悬浮窗 可拖动,大小的视图,可放置在屏幕边缘. 在使用 ...

随机推荐

  1. php大力力 [046节] 兄弟连高洛峰 PHP教程 2015年[最新最新最新最新最新]

    兄弟连高洛峰老师新版PHP视频教程列表[每日更新] http://bbs.lampbrother.net/read-htm-tid-160506.html HTML部分1.[2015]兄弟连高洛峰 H ...

  2. 在 C++ 代码中使用 UE4 插件---Using a plugin in C++ code

    例如使用 CustomMeshComponent 插件 在Build.cs 文件 中 添加以下两行代码 如图可配置路径,可解决#include "CustomMeshComponent&qu ...

  3. 浅谈SEO-提交(一)

    前段时间,花了点时间研究了下SEO,Search Engine Optimization,百度百科这样描述: 中文意译为“搜索引擎优化”.SEO是指通过对网站内部调整优化及站外优化,使网站满足搜索引擎 ...

  4. TCP三次握手及四次挥手详细图解

    TCP三次握手及四次挥手详细图解 Andrew Huangbluedrum@163.com    相对于SOCKET开发者,TCP创建过程和链接折除过程是由TCP/IP协议栈自动创建的.因此开发者并不 ...

  5. Scalding初探之番外篇:Mac OS下的安装

    把你从写繁琐的Map-reduce Job中解放出来,写分布式跟写本地程序没两样,Scalding真真代表着先进生产力的方向啊 心动不如行动,赶紧装一个吧 1 安装JDK 2 安装Homebrew r ...

  6. c数据结构栈的基本操作(字符逆序输出)

    线性栈 输入字符,再输出 #include "stdafx.h" #include<stdlib.h> #include<malloc.h> #define ...

  7. 手机自适应meta设置

    <meta name="format-detection" content="telephone=no"><meta name="v ...

  8. IncDec Sequence

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=3043[题目描述]给定一个长度为 n 的数列{a1,a2...an},每次可以选择一个区间[ ...

  9. 关于 MaxScript 获取所有贴图

    相关内容记录在官方文档 BitmapTexture : TextureMap 中 fn allUsedMaps = ( sceneMaps = usedMaps() for m in meditmat ...

  10. Unable to create a constant value of type 'Closure type'

    使用Linq to Entities的时候发生如下异常: Unable to create a constant value of type 'Closure type'. Only primitiv ...