IOS UI-瀑布流(UICollectionView)
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)的更多相关文章
- IOS 瀑布流UICollectionView实现
IOS 瀑布流UICollectionView实现 在实现瀑布流之前先来看看瀑布流的雏形(此方法的雏形 UICollectionView) 对于UICollectionView我们有几点注意事项 它和 ...
- iOS横向瀑布流的封装
前段时间, 做一个羡慕, 需要使用到瀑布流! 说道瀑布流, 或许大家都不陌生, 瀑布流的实现也有很多种! 从scrollView 到 tableView 书写的瀑布流, 然后再到2012年iOS6 苹 ...
- ios图片瀑布流代码
ios瀑布流,实现简单的瀑布流视图布局,可以显示网络图片,下拉刷新,上拉加载更多. 下载:http://www.huiyi8.com/sc/9087.html
- iOS基础 - 瀑布流
一.瀑布流简介 瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pint ...
- ios开发瀑布流框架的应用
一:瀑布流框架的应用:将封装好的瀑布流框架导入,遵守协议 二:代码: #import "HMShopsViewController.h" #import "HMShopC ...
- ios开发瀑布流框架的封装
一:瀑布流框架封装的实现思路:此瀑布流框架的封装仿照tableView的底层实现,1:每个cell的frame的设置都是找出每列的最大y值,比较每列的最大y值,将下一个cell放在最大y值最小的那一列 ...
- iOS开发瀑布流的实现
//自定义布局,继承于UICollectionViewLayout #import <UIKit/UIKit.h> @class WaterFlowLayout; @protocol W ...
- iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流
上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...
- iOS开发-UICollectionView实现瀑布流
关于瀑布流的实现网上有很多种解法,自定义控件,TableView+ScrollView,UICollectionView是iOS6发布之后用于展示集合视图,算起来已经发布三年左右了,不过知识点是不变的 ...
- iOS - UICollectionView 瀑布流 添加表头视图的坑
UICollectionView 瀑布流 添加表头视图的坑 首先是,需求加了个头视图在顶部,在collectionView中的头视图跟TableView的不一样,TableView的表头只要设置tab ...
随机推荐
- android 如何获取当前的Activity类名
比如友盟统计页面停留时间,咱们需要知道当前页面停留了多久. 一般我们都有一个父类Activity,用下面的方法可以获得完整的包名.类名结构 this.getLocalClassName() 输出如下: ...
- Windows 配置安卓环境变量
变量名:JAVA_HOME 变量值:JDK 路径 变量名:CLASSPATH 变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\toos.jar // ...
- Python面试题之Python生成器
首先说明一下生成器也是迭代器,也有迭代器的那些优点. 那为什么要生成器呢?因为到目前为止都 不是你写的迭代器,都是别人定义好的.那如何自己去造一个迭代器呢?下面的内容就会给你答案. 想要自己造一个迭代 ...
- 20145309java第三次实验报告
实验三 敏捷开发与XP实践 实验内容 •下载并学会使用git上传代码: •与同学结对,相互下载并更改对方代码,并上传: •实现代码的重载. 实验步骤 下载并用git上传代码: •1.下载并安装好git ...
- Maven错误recv failed
问题: 从SVN上检出了一个Maven项目,在执行clean命令时,出现如下错误: java.net.SocketException:Software caused connection ab ...
- Flutter中集成Font Awesome
1.添加引用 在 pubspec.yaml文件中,加入 font awesome的引用 dependencies: flutter: sdk: flutter # The following adds ...
- c语言数组拷贝
#include <string.h> // 如果要从数组a复制k个元素到数组b,可以这样做 memcpy(b,a,sizeof(int)*k);
- Swift学习笔记 - Swift属性只读
在OC中我们经常用到只读属性,用readonly修饰一下就行了,但在Swift中已经不是这样修饰的了,下面记录一下Swift中只读属性的使用 在OC中的只读: //只读属性 @property(rea ...
- PHP 根据IP地址获取所在城市
header('Content-Type:text/html;Charset=utf-8'); function GetIp(){ $realip = ''; $unknown = 'unknown' ...
- 安装配置mariadb-10.1.19
本文参考:http://chenzehe.iteye.com/blog/1266260 感谢原作者的分享! 首先安装/更新一些编译时会用到的基础包 [root@localhost local]# y ...