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 添加自己的工具包

    一. 在添加工具包前环境变量要定位到当前目录, export CLASSPATH=.:/home/share/ 添加工具类 我的目录\\192.168.1.101\share\share\net\fe ...

  2. 【招聘】这一次,我们Hold住了世界杯

    作为国内最大的云计算服务商,阿里云在视频领域拥有绝对的技术优势,全球范围内拥有1500多个CDN节点,带宽储备120多T,不仅为优酷.CNTV.CCTV5提供技术支撑,还承担了全网70%的世界杯流量. ...

  3. 关于NOIP2018初赛

    题面 这次PJ初赛有点傻了,可能是因为兴华水土不服吧(在这荒度了六年级的光阴). 选择题 DDDBBAAAABABBBB 第四题 当时懵了,我啥也不知道,于是就开始蒙 A.LAN B.WAN C.MA ...

  4. Java编程的逻辑 (6) - 如何从乱码中恢复 (上)?

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

  5. HTML5练习3

    1.输入问题,判断答案,按钮倒计时 主要代码: <!doctype html> <html> <head> <meta charset="utf-8 ...

  6. 【LOJ】#2495. 「AHOI / HNOI2018」转盘

    题面 题解 考虑我肯定是从一个人出发,开始依次标记,而不会跳过某个人,因为如果我跳过了,那么我之后回来还需要标记它,比不上我等完它再一直走到最后(因为多了走一圈之后走回它的代价) 我们倍长整个序列,我 ...

  7. java学习之租车系统

    ​背景:有三种类型的车供给用户来租用​ ​要求:控制台用户交互界面,根据用户需求输出租车价格,结果如下: 创建租车类主要设计过程: 创建租车类 创建Car父类,包含四种属性成员,重写构造方法 创建三种 ...

  8. 七夕情人节表白-纯JS实现3D心形+图片旋转

    七夕情人节就快到了,这里献上纯js表白神器-心里都是你,预览: 技术点:css-3d.js-随机色.js-transform 1.html: <div class="heart&quo ...

  9. codevs 2821 天使之城

    题目描述 Description 天使城有一个火车站,每辆火车都从A方向驶入车站,再从B方向驶出车站. 为了调度火车,火车站设有停放轨道,可存放5辆火车.已知从A进入车站顺序为1.2.3…….现在给你 ...

  10. ios 从工程中删除Cocoapods

    删除工程文件夹下的Podfile.Podfile.lock及Pods文件夹 2. 删除xcworkspace文件 3. 使用xcodeproj文件打开工程,删除Frameworks组下的Pods.xc ...