UICollectionViewFlowLayout使用示例

效果

源码

https://github.com/YouXianMing/iOS-Project-Examples

//
// ViewController.m
// LayoutViewController
//
// Created by YouXianMing on 16/5/3.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "UIView+SetRect.h"
#import "RootModel.h"
#import "FlowStyleCell.h"
#import "CollectionHeaderView.h"
#import "CollectionBottomView.h" @interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> @property (nonatomic, strong) RootModel *rootModel;
@property (nonatomic, strong) UICollectionView *collectionView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createDataSource]; [self createCollectionView];
} #pragma mark - Create data source. - (void)createDataSource { NSDictionary *datas = @{@"response" : @YES,
@"datas" : @[@{@"title" : @"Header1",
@"subTitle" : @"Bottom1",
@"infos" : @[@{@"name" : @""}, @{@"name" : @""},
@{@"name" : @""}, @{@"name" : @""},
@{@"name" : @""}, @{@"name" : @""},
@{@"name" : @""}, @{@"name" : @""},
@{@"name" : @""}]},
@{@"title" : @"Header2",
@"subTitle" : @"Bottom2",
@"infos" : @[@{@"name" : @""}, @{@"name" : @""},
@{@"name" : @""}, @{@"name" : @""},
@{@"name" : @""}, @{@"name" : @""}]},
@{@"title" : @"Header3",
@"subTitle" : @"Bottom3",
@"infos" : @[@{@"name" : @""}, @{@"name" : @""},
@{@"name" : @""}, @{@"name" : @""},
@{@"name" : @""}, @{@"name" : @""},
@{@"name" : @""}, @{@"name" : @""}]}]};
self.rootModel = [[RootModel alloc] initWithDictionary:datas];
} #pragma mark - Create UICollectionView. - (void)createCollectionView { self.view.backgroundColor = [UIColor whiteColor]; // Setup UICollectionViewFlowLayout.
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
layout.itemSize = CGSizeMake(, );
layout.sectionInset = UIEdgeInsetsMake(, , , );
layout.minimumInteritemSpacing = .f; // 横向排列最小间距
layout.minimumLineSpacing = 5.0f; // 纵向排列最小间距 // Init UICollectionView.
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self; // Register CollectionHeaderView & CollectionBottomView.
[self.collectionView registerClass:[FlowStyleCell class] forCellWithReuseIdentifier:@"FlowStyleCell"];
[self.collectionView registerClass:[CollectionHeaderView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView"];
[self.collectionView registerClass:[CollectionBottomView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionBottomView"]; [self.view addSubview:self.collectionView];
} #pragma mark - UICollectionView's delegate & dataSource. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { DatasModel *dataModel = self.rootModel.datas[section]; return dataModel.infos.count;
} - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return self.rootModel.datas.count;
} - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { DatasModel *dataModel = self.rootModel.datas[indexPath.section];
InfoModel *infoModel = dataModel.infos[indexPath.row]; FlowStyleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FlowStyleCell" forIndexPath:indexPath];
cell.data = infoModel;
cell.indexPath = indexPath;
[cell loadContent]; return cell;
} - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { CustomCollectionReusableView *reusableView = nil; if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionHeaderView"
forIndexPath:indexPath]; DatasModel *dataModel = self.rootModel.datas[indexPath.section];
reusableView.data = dataModel;
reusableView.indexPath = indexPath;
[reusableView loadContent]; } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) { reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionBottomView"
forIndexPath:indexPath]; DatasModel *dataModel = self.rootModel.datas[indexPath.section];
reusableView.data = dataModel;
reusableView.indexPath = indexPath;
[reusableView loadContent];
} return reusableView;
} #pragma mark - UICollectionViewDelegateFlowLayout. - (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { // If you have headerView, you must implement this method.
return CGSizeMake(Width, );
} - (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { // If you have footerView, you must implement this method.
return CGSizeMake(Width, );
} #pragma mark - Hide statusBar. - (BOOL)prefersStatusBarHidden { return YES;
} @end

细节

itemSize的含义

minimumLineSpacing的含义

minimunInteritemSpacing的用途

UICollectionViewFlowLayout使用示例的更多相关文章

  1. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  2. .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1

    微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...

  3. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  4. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九)   ...

  5. JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

  6. XAMARIN ANDROID 二维码扫描示例

    现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile  做一个简单的 Android 条码扫描示 ...

  7. iOS之ProtocolBuffer搭建和示例demo

    这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...

  8. Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)

    示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...

  9. oracle常用函数及示例

    学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...

随机推荐

  1. Java编程的逻辑 (10) - 强大的循环

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  2. shell学习(四)

    一.字符截取 expr 基本用法 expr  substr   $var1   起始位置    截取长度,如: [root@localhost mnt]# a=Centos6.9[root@local ...

  3. Jupyter notebook安装与使用

    Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言. 安装 安装python 3 pip安装 pip3 install - ...

  4. 【LOJ】#2107. 「JLOI2015」城池攻占

    题解 用一个平衡树维护能攻占到u点的骑士,合并到父亲的时候去掉攻击力小于父亲生命值的那部分,只要把那棵树拆掉并且将树中的所有骑士更新一下答案,用无旋式treap很好写 合并的时候只要启发式合并就可以了 ...

  5. python 实现远端ftp文件上传下载

    python 实现ftp上传下载 * 脚本需要传入两个参数,参数1为需要从远端ftp站点下载文件名称,参数2为已知需要下载的文件md5值,文件下载完成后会自动进行md5值校验 * 运行示例 [root ...

  6. linux学习笔记-5.用户和组

    1.添加一个tom用户,设置它属于users组,并添加注释信息 分步完成: useradd tom usermod -g users tom usermod -c "hr tom" ...

  7. MySql 时间戳存char还是存int?

    一次小事故,让我对时间戳存char还是存int有了深刻的印象. 生产环境的sql条件涉及到时间戳字段的大小比较(between and),当时设计的时间戳类型是char(10),结果当数据量达到200 ...

  8. clob字段超过4000转String类型

    上次提到listagg()和wm_concat()方法合并过的字段类型为clob,要是字段长度超过4000,直接使用to_char()方法转会报错. 解决方法可以在java代码中使用流的方式转化成字符 ...

  9. 【BZOJ 1005】 1005: [HNOI2008]明明的烦恼 (prufer数列+高精度)

    1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4981  Solved: 1941 Description ...

  10. Codeforces.835E.The penguin's game(交互 按位统计 二分)

    题目链接 \(Description\) 有一个长为\(n\)的序列,其中有两个元素为\(y\),其余全为\(x\).你可以进行\(19\)次询问,每次询问你给出一个下标集合,交互库会返回这些元素的异 ...