ViewController.m

 //
// ViewController.m
// IOS_0227_瀑布流
//
// Created by ma c on 16/2/27.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import "WaterFlowLayout.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "HMShop.h"
#import "ShopCell.h" @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,WaterFlowLayoutDelegate> @property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *shops; @end @implementation ViewController static NSString *ID = @"shop"; - (void)viewDidLoad {
[super viewDidLoad]; //初始化数据
NSArray *shopArray = [HMShop objectArrayWithFilename:@"1.plist"];
[self.shops addObjectsFromArray:shopArray]; [self createUI];
} - (NSMutableArray *)shops
{
if (!_shops) {
_shops = [NSMutableArray array];
}
return _shops;
} - (void)createUI
{
// CGRect rect = CGRectMake(7, 100, 400, 200);
WaterFlowLayout *layout = [[WaterFlowLayout alloc] init];
UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
collection.backgroundColor = [UIColor groupTableViewBackgroundColor];
layout.delegate = self;
collection.dataSource = self;
collection.delegate = self;
[collection registerNib:[UINib nibWithNibName:@"ShopCell" bundle:nil] forCellWithReuseIdentifier:ID];
[self.view addSubview:collection];
self.collectionView = collection;
[self.collectionView addFooterWithTarget:self action:@selector(loadMoreShops)];
} - (void)loadMoreShops
{
//初始化数据
NSArray *shopArray = [HMShop objectArrayWithFilename:@"1.plist"];
[self.shops addObjectsFromArray:shopArray]; [self.collectionView reloadData]; [self.collectionView footerEndRefreshing];
} #pragma mark - WaterFlowLayoutDelegate - (CGFloat)waterFlowLayout:(WaterFlowLayout *)waterFlowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath
{
HMShop *shop = self.shops[indexPath.item];
return shop.height / shop.width * width;
} #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.shops.count;
} - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; cell.shop = self.shops[indexPath.item]; return cell;
} @end

WaterFlowLayout.m

 //
// WaterFlowLayout.h
// IOS_0227_瀑布流
//
// Created by ma c on 16/2/27.
// Copyright © 2016年 博文科技. All rights reserved.
// #import <UIKit/UIKit.h>
@class WaterFlowLayout; @protocol WaterFlowLayoutDelegate <NSObject> - (CGFloat)waterFlowLayout:(WaterFlowLayout *)waterFlowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath; @end @interface WaterFlowLayout : UICollectionViewLayout @property (nonatomic, assign) UIEdgeInsets sessionInset;
//列间距
@property (nonatomic, assign) CGFloat columnMargin;
//行间距
@property (nonatomic, assign) CGFloat rowMargin;
//列数
@property (nonatomic, assign) int columnCount; @property (nonatomic, strong) id<WaterFlowLayoutDelegate> delegate; @end //
// WaterFlowLayout.m
// IOS_0227_瀑布流
//
// Created by ma c on 16/2/27.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "WaterFlowLayout.h" @interface WaterFlowLayout ()
//存储每列最大高度Y值
@property (nonatomic, strong) NSMutableDictionary *maxYDict;
@property (nonatomic, strong) NSMutableArray *attrsArray; @end @implementation WaterFlowLayout - (instancetype)init
{
self = [super init];
if (self) {
self.rowMargin = ;
self.columnMargin = ;
self.sessionInset = UIEdgeInsetsMake(, , , );
self.columnCount = ;
}
return self;
} - (NSMutableDictionary *)maxYDict
{
if (!_maxYDict) {
_maxYDict = [[NSMutableDictionary alloc] init];
for (int i = ; i < self.columnCount; i++) {
NSString *column = [NSString stringWithFormat:@"%d",i];
_maxYDict[column] = @(self.sessionInset.top);
//NSLog(@"%@",_maxYDict); }
}
return _maxYDict;
} - (NSMutableArray *)attrsArray
{
if (!_attrsArray) {
_attrsArray = [NSMutableArray array];
}
return _attrsArray;
} //布局前准备
- (void)prepareLayout
{
[super prepareLayout];
//NSLog(@"layoutAttributesForElementsInRect"); //清空最大Y值
for (int i = ; i < self.columnCount; i++) {
NSString *column = [NSString stringWithFormat:@"%d",i];
_maxYDict[column] = @;
}
//计算item属性
[self.attrsArray removeAllObjects];
NSInteger count = [self.collectionView numberOfItemsInSection:]; for (int i=; i<count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:];
UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
[self.attrsArray addObject:attrs];
}
} - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
} //尺寸
- (CGSize)collectionViewContentSize
{
//NSLog(@"collectionViewContentSize");
//假设最长的那一列是第0列
__block NSString *maxYColumn = @"";
//找出最长的那一列
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj floatValue] > [self.maxYDict[maxYColumn] floatValue]) {
maxYColumn = key;
}
}];
return CGSizeMake(, [self.maxYDict[maxYColumn] floatValue] + self.sessionInset.bottom);
}
//rect范围内的布局属性
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
// //NSLog(@"layoutAttributesForElementsInRect");
//
// for (int i = 0; i < self.columnCount; i++) {
// NSString *column = [NSString stringWithFormat:@"%d",i];
// _maxYDict[column] = @0;
// }
//
// NSMutableArray *array = [NSMutableArray array];
// NSInteger count = [self.collectionView numberOfItemsInSection:0];
//
// for (int i=0; i<count; i++) {
// NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
// UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
// [array addObject:attrs];
//
// }
// return array;
return self.attrsArray;
}
//indexPath位置的item的布局属性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"layoutAttributesForItemAtIndexPath");
//假设最短的那一列是第0列
__block NSString *minYColumn = @"";
//找出最短的那一列
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj floatValue] < [self.maxYDict[minYColumn] floatValue]) {
minYColumn = key;
}
}];
//NSLog(@"%@",minYColumn); //计算尺寸
CGFloat width = (self.collectionView.frame.size.width - self.sessionInset.left - self.sessionInset.right - (self.columnCount - ) * self.columnMargin) / self.columnCount;
CGFloat height = [self.delegate waterFlowLayout:self heightForWidth:width atIndexPath:indexPath]; //计算位置
CGFloat x = self.sessionInset.left + (width + self.columnMargin) * [minYColumn intValue];
CGFloat y = [self.maxYDict[minYColumn] floatValue] + self.rowMargin; //更新这一列最大Y值
self.maxYDict[minYColumn] = @(y + height); //创建属性
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attrs.frame = CGRectMake(x, y, width, height); return attrs;
}
@end

ShopCell.m

 //
// ShopCell.h
// IOS_0227_瀑布流
//
// Created by ma c on 16/2/27.
// Copyright © 2016年 博文科技. All rights reserved.
// #import <UIKit/UIKit.h>
@class HMShop;
@interface ShopCell : UICollectionViewCell @property (nonatomic, strong) HMShop *shop; @end //
// ShopCell.m
// IOS_0227_瀑布流
//
// Created by ma c on 16/2/27.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ShopCell.h"
#import "HMShop.h" @interface ShopCell () @property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *lblPrice; @end @implementation ShopCell - (void)awakeFromNib {
// Initialization code
} - (void)setShop:(HMShop *)shop
{
_shop = shop; self.imgView.image = [UIImage imageNamed:shop.icon];
self.lblPrice.text = shop.price; } @end

HMShop.m

 //
// HMShop.h
// 05-黑马瀑布流
//
// Created by Romeo on 15/11/25.
// Copyright © 2015年 itheima. All rights reserved.
// #import <UIKit/UIKit.h> @interface HMShop : NSObject
// 图片
@property (nonatomic, copy) NSString *icon;
// 价格
@property (nonatomic, copy) NSString *price;
// 图片的真实高度
@property (nonatomic, assign) CGFloat height;
// 图片的真实宽度
@property (nonatomic, assign) CGFloat width; + (instancetype)shopWithDict:(NSDictionary *)dict; @end //
// HMShop.m
// 05-黑马瀑布流
//
// Created by Romeo on 15/11/25.
// Copyright © 2015年 itheima. All rights reserved.
// #import "HMShop.h" @implementation HMShop + (instancetype)shopWithDict:(NSDictionary *)dict {
id obj = [[self alloc] init];
[obj setValuesForKeysWithDictionary:dict];
return obj;
} @end

IOS UI-瀑布流(UICollectionView)的更多相关文章

  1. IOS 瀑布流UICollectionView实现

    IOS 瀑布流UICollectionView实现 在实现瀑布流之前先来看看瀑布流的雏形(此方法的雏形 UICollectionView) 对于UICollectionView我们有几点注意事项 它和 ...

  2. iOS横向瀑布流的封装

    前段时间, 做一个羡慕, 需要使用到瀑布流! 说道瀑布流, 或许大家都不陌生, 瀑布流的实现也有很多种! 从scrollView 到 tableView 书写的瀑布流, 然后再到2012年iOS6 苹 ...

  3. ios图片瀑布流代码

    ios瀑布流,实现简单的瀑布流视图布局,可以显示网络图片,下拉刷新,上拉加载更多. 下载:http://www.huiyi8.com/sc/9087.html

  4. iOS基础 - 瀑布流

    一.瀑布流简介 瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pint ...

  5. ios开发瀑布流框架的应用

    一:瀑布流框架的应用:将封装好的瀑布流框架导入,遵守协议 二:代码: #import "HMShopsViewController.h" #import "HMShopC ...

  6. ios开发瀑布流框架的封装

    一:瀑布流框架封装的实现思路:此瀑布流框架的封装仿照tableView的底层实现,1:每个cell的frame的设置都是找出每列的最大y值,比较每列的最大y值,将下一个cell放在最大y值最小的那一列 ...

  7. iOS开发瀑布流的实现

    //自定义布局,继承于UICollectionViewLayout #import <UIKit/UIKit.h> @class WaterFlowLayout; @protocol  W ...

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

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

  9. iOS开发-UICollectionView实现瀑布流

    关于瀑布流的实现网上有很多种解法,自定义控件,TableView+ScrollView,UICollectionView是iOS6发布之后用于展示集合视图,算起来已经发布三年左右了,不过知识点是不变的 ...

  10. iOS - UICollectionView 瀑布流 添加表头视图的坑

    UICollectionView 瀑布流 添加表头视图的坑 首先是,需求加了个头视图在顶部,在collectionView中的头视图跟TableView的不一样,TableView的表头只要设置tab ...

随机推荐

  1. Leetcode 236

    思路:1.如果p或q就是根节点,那么LCA=p或q,返回根节点(递归出口) 2.分治 2.1 Divide:分别计算左字树和右子树的LCA 2.2 Conquer:如果左字树和右子树的计算结果均不为空 ...

  2. matlab和mathematics最新的FTP地址

    https://dio.obspm.fr/interne/logiciels/matlab/ 分享一个地址,非常好的FTP网站.

  3. 【android】使用RecyclerView和CardView,实现知乎日报精致布局

    完整代码,请参考我的博客园客户端,git地址:http://git.oschina.net/yso/CNBlogs 在写博客园客户端的时候,突然想到,弄个知乎日报风格的简单清爽多好!不需要那么多繁杂的 ...

  4. CCTableView

    今天用到TableView, 我就来记录一下....这些都是在网上找到了资料 //首先 继承 : public cocos2d::extension::CCTableViewDelegate,publ ...

  5. 阿里云ECS服务器磁盘挂载(转)

    买了阿里云的ECS云服务器,本机赠送20GB的磁盘,感觉不够用,又买了一块500GB的磁盘,本文就是记录怎么把这500GB的磁盘挂载上. 检查现在磁盘情况 我们可以看到买的那个500GB的磁盘没有出现 ...

  6. [Android] 录音与播放录音实现

    http://blog.csdn.net/cxf7394373/article/details/8313980 android开发文档中有一个关于录音的类MediaRecord,一张图介绍了基本的流程 ...

  7. spring项目gitignore

    target/ ### STS ### .apt_generated .classpath .factorypath .project .settings .springBeans ### Intel ...

  8. Android执行shell命令 top ps

    Android执行shell命令 一.方法 /** * 执行一个shell命令,并返回字符串值 * * @param cmd * 命令名称&参数组成的数组(例如:{"/system/ ...

  9. 重新想,重新看——CSS3变形,过渡与动画②

    本篇文章主要用来归纳总结CSS3变形属性. CSS3变形属性大致可以分为以下三个部分: 变形控制属性 2D变形函数 3D变形函数 下面将对其一一进行分析: 1.变形控制属性 所谓的变形控制属性主要指“ ...

  10. 无线网卡在 MAC 系统下的安装与使用过程

    MAC系统安装netgear无线网卡的方法: 1)去网件官网下载相应的驱动软件 2)单击页面左侧的“Version 1.0.0.0”进入下载页面如下图 3)选择对应您系统版本的驱动程序,按右键保存到计 ...