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的用途

最新文章

  1. instanceof关键字
  2. CentOS详解top命令各个数据的含义
  3. 【转】sublime text 2 下的Markdown写作
  4. isKindOfClass、isMemberOfClass的区别
  5. C# AES,AesManaged使用学习
  6. UVA11552------FEWEST FLOPS------区间型的DP
  7. Android 网络编程 Socket Http
  8. hdu 3065 AC自动机(各子串出现的次数)
  9. 26 , CSS 构造表单
  10. HTML5_canvas_填充文本,描边文本
  11. hdu 1241(DFS/BFS)
  12. 016 SpringMVC中重定向
  13. Ubuntu 13.10 解决虚拟机摄像头无法使用问题
  14. CentOS7 设置软件镜像源
  15. 字体图标,盒子显隐,overflow属性,伪类设计边框,盒子阴影2d形变
  16. Router pipeline
  17. Java NIO 基础知识
  18. mvc 提交Html内容的处理
  19. Spring cloud子项目
  20. CPU与GPU区别大揭秘

热门文章

  1. PHP 获取某年第几周的开始日期和结束日期的实例
  2. LOJ 10160 - 「一本通 5.2 练习 3」周年纪念晚会 / 没有上司的晚会
  3. KnockoutJs学习笔记(十)
  4. ***Bootstrap FileInput插件的使用经验汇总
  5. day--14前端(HTML、CSS)
  6. stm32+ESP8266AT指令详细说明
  7. 【noip模拟赛1】古韵之同心锁
  8. 015 jquery中包裹节点
  9. Java对epub电子书类型切割
  10. opesntack 底层共享存储 迁移配置