UICollectionViewFlowLayout使用示例
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使用示例的更多相关文章
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...
- 通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...
- WCF学习之旅—第三个示例之四(三十)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) ...
- JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...
- XAMARIN ANDROID 二维码扫描示例
现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile 做一个简单的 Android 条码扫描示 ...
- iOS之ProtocolBuffer搭建和示例demo
这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...
- oracle常用函数及示例
学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...
随机推荐
- java 内部类使用 .this 和 .new
如果需要生成对外部类对象的引用,可以使用外部类的名字后面紧跟圆点和this,这样产生的引用自动地具有正确的类型,这一点在编译器就被知晓并受到检查,因此并没有运行时开销 //: innerclasses ...
- java innerclasses(内部类)
可以将一个类定义在另一个类的内部,这就是内部类 创建内部类的方式---把类的定义置于外部类的里面 典型的情况是,外部类将有一个方法,该方法返回一个指向内部类的引用,就像在to()和contents() ...
- js如何判断访问来源是来自搜索引擎(蜘蛛人)还是直接访问
以下javascript脚本代码可以实现判断访问是否来自搜索引擎.代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <scri ...
- MAC下代理工具Charles使用
一.跟踪HTTPS 1.下载官方的证书ssl.zip证书,解压成*.crt 2.可以通过邮箱或者发布到自己的服务器的方式,然后用手机去下载安装crt文件. 3.charles设置Proxy--> ...
- 从 prototype.js 深入学习 javascript 的面向对象特性
从 prototype.js 深入学习 javascript 的面向对象特性 js是一门很强大的语言,灵活,方便. 目前我接触到的语言当中,从语法角度上讲,只有 Ruby 比它更爽. 不过我接触的动态 ...
- 【LOJ】#2512. 「BJOI2018」链上二次求和
题面 题解 转化一下可以变成所有小于等于r的减去小于等于l - 1的 然后我们求小于等于x的 显然是 \(\sum_{i = 1}^{n} \sum_{j = 1}^{min(i,x)} sum[i] ...
- 关于伪类“:pseudo-class”和伪元素“::pseudo-element”的常见应用
伪类用于指定要选择的元素的特殊状态,向其添加特殊的效果,比如: input { width: 515px; height: 50px; padding: 10px 20px; border: 1px ...
- Java 内存模型 ,一篇就够了!
Java 虚拟机 我们都知道 Java 语言的可以跨平台的,这其中的核心是因为存在 Java 虚拟机这个玩意.虚拟机,顾名思义就是虚拟的机器,这不是真实存在的硬件,但是却可以和不同的底层平台进行交 ...
- 初识MYSQL2
mysql的配置 MySql默认的端口号是3306 默认字符集的设置 在mysql的安装目录,会看到my.ini文件! my.ini文件介绍 01.default-character-set=utf8 ...
- jquery获取系统当前时间
//判断是否在前面加0function getNow(s) { return s < 10 ? '0' + s: s;} var myDate = new Date(); var year=my ...