一:瀑布流框架的应用:将封装好的瀑布流框架导入,遵守协议

二:代码:

 #import "HMShopsViewController.h"
#import "HMShopCell.h"
#import "HMWaterflowView.h"
#import "HMShop.h"
#import "MJExtension.h"
#import "MJRefresh.h" @interface HMShopsViewController ()<HMWaterflowViewDataSource, HMWaterflowViewDelegate>
@property (nonatomic, strong) NSMutableArray *shops;
@property (nonatomic, weak) HMWaterflowView *waterflowView;
@end @implementation HMShopsViewController - (NSMutableArray *)shops
{
if (_shops == nil) {
self.shops = [NSMutableArray array];
}
return _shops;
} - (void)viewDidLoad
{
[super viewDidLoad]; // 0.初始化数据
NSArray *newShops = [HMShop objectArrayWithFilename:@"2.plist"];
[self.shops addObjectsFromArray:newShops]; // 1.瀑布流控件
HMWaterflowView *waterflowView = [[HMWaterflowView alloc] init];
waterflowView.backgroundColor = [UIColor redColor];
// 跟随着父控件的尺寸而自动伸缩
waterflowView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
waterflowView.frame = self.view.bounds;
waterflowView.dataSource = self;
waterflowView.delegate = self;
[self.view addSubview:waterflowView];
self.waterflowView = waterflowView; // 2.继承刷新控件
// [waterflowView addFooterWithCallback:^{
// NSLog(@"进入上拉加载状态");
// }]; // [waterflowView addHeaderWithCallback:^{
// NSLog(@"进入下拉加载状态");
// }]; [waterflowView addHeaderWithTarget:self action:@selector(loadNewShops)];
[waterflowView addFooterWithTarget:self action:@selector(loadMoreShops)];
} - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// NSLog(@"屏幕旋转完毕");
[self.waterflowView reloadData];
} - (void)loadNewShops
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 加载1.plist
NSArray *newShops = [HMShop objectArrayWithFilename:@"1.plist"];
[self.shops insertObjects:newShops atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(, newShops.count)]];
}); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 刷新瀑布流控件
[self.waterflowView reloadData]; // 停止刷新
[self.waterflowView headerEndRefreshing];
});
} - (void)loadMoreShops
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 加载3.plist
NSArray *newShops = [HMShop objectArrayWithFilename:@"3.plist"];
[self.shops addObjectsFromArray:newShops];
}); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 刷新瀑布流控件
[self.waterflowView reloadData]; // 停止刷新
[self.waterflowView footerEndRefreshing];
});
} #pragma mark - 数据源方法
- (NSUInteger)numberOfCellsInWaterflowView:(HMWaterflowView *)waterflowView
{
return self.shops.count;
} - (HMWaterflowViewCell *)waterflowView:(HMWaterflowView *)waterflowView cellAtIndex:(NSUInteger)index
{
HMShopCell *cell = [HMShopCell cellWithWaterflowView:waterflowView]; cell.shop = self.shops[index]; return cell;
} - (NSUInteger)numberOfColumnsInWaterflowView:(HMWaterflowView *)waterflowView
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
// 竖屏
return ;
} else {
return ;
}
} #pragma mark - 代理方法
- (CGFloat)waterflowView:(HMWaterflowView *)waterflowView heightAtIndex:(NSUInteger)index
{
HMShop *shop = self.shops[index];
// 根据cell的宽度 和 图片的宽高比 算出 cell的高度
return waterflowView.cellWidth * shop.h / shop.w;
}
@end

知识点分析:1:利用MJEXtension将plist文件转化成模型数组:NSArray *newShops = [HMShop objectArrayWithFilename:@"2.plist"]; [self.shops addObjectsFromArray:newShops];2:向数据源中添加数据:addObjectsFromArray , insertObject atIndexes :NSArray *newShops = [HMShop objectArrayWithFilename:@"1.plist"]; [self.shops insertObjects:newShops atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, newShops.count)]];

2:监听屏幕的旋转:在控制器中实现didRotateFromInterfaceOrientation,可以实现对屏幕旋转的监听,此时设置waterFlow的autoresizingMask属性,设置其宽高随父视图宽高自由伸缩,waterflowView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;在返回列数的代理方法中,根据屏幕的方向,来确定列数

if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {

// 竖屏

return 3;

} else {

return 5;

}

3:MJRefresh:添加上拉刷新,下拉加载更多。两种方法1:block回调的方式 2:addTarget 方式来添加 。停止刷新:

[self.waterflowView headerEndRefreshing];

[self.waterflowView footerEndRefreshing];

4:GCD执行一次函数:

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

// 加载3.plist

NSArray *newShops = [HMShop objectArrayWithFilename:@"3.plist"];

[self.shops addObjectsFromArray:newShops];

});

5:在返回cell高度的方法中:要根据cell的宽度 和 图片的宽高比 算出 cell的高度。

HMShop *shop = self.shops[index];

// 根据cell的宽度 和 图片的宽高比 算出 cell的高度

return waterflowView.cellWidth * shop.h / shop.w;

6:cell的封装

 #import "HMWaterflowViewCell.h"
@class HMWaterflowView, HMShop; @interface HMShopCell : HMWaterflowViewCell
+ (instancetype)cellWithWaterflowView:(HMWaterflowView *)waterflowView; @property (nonatomic, strong) HMShop *shop;
@end
 #import "HMShopCell.h"
#import "HMWaterflowView.h"
#import "UIImageView+WebCache.h"
#import "HMShop.h" @interface HMShopCell()
@property (weak, nonatomic) UIImageView *imageView;
@property (weak, nonatomic) UILabel *priceLabel;
@end @implementation HMShopCell + (instancetype)cellWithWaterflowView:(HMWaterflowView *)waterflowView
{
static NSString *ID = @"SHOP";
HMShopCell *cell = [waterflowView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[HMShopCell alloc] init];
cell.identifier = ID;
}
return cell;
} - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) { UIImageView *imageView = [[UIImageView alloc] init];
[self addSubview:imageView];
self.imageView = imageView; UILabel *priceLabel = [[UILabel alloc] init];
priceLabel.backgroundColor = [UIColor colorWithRed: green: blue: alpha:0.3];
priceLabel.textAlignment = NSTextAlignmentCenter;
priceLabel.textColor = [UIColor whiteColor];
[self addSubview:priceLabel];
self.priceLabel = priceLabel;
}
return self;
} - (void)setShop:(HMShop *)shop
{
_shop = shop; self.priceLabel.text = shop.price;
[self.imageView sd_setImageWithURL:[NSURL URLWithString:shop.img] placeholderImage:[UIImage imageNamed:@"loading"]];
} - (void)layoutSubviews
{
[super layoutSubviews]; self.imageView.frame = self.bounds; CGFloat priceX = ;
CGFloat priceH = ;
CGFloat priceY = self.bounds.size.height - priceH;
CGFloat priceW = self.bounds.size.width;
self.priceLabel.frame = CGRectMake(priceX, priceY, priceW, priceH);
} @end

7:model的封装

 #import <Foundation/Foundation.h>

 @interface HMShop : NSObject
@property (nonatomic, assign) CGFloat w;
@property (nonatomic, assign) CGFloat h;
@property (nonatomic, copy) NSString *img;
@property (nonatomic, copy) NSString *price;
@end
 #import "HMShop.h"

 @implementation HMShop

 @end

ios开发瀑布流框架的应用的更多相关文章

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

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

  2. iOS开发瀑布流的实现

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

  3. iOS 开发之照片框架详解(2)

    一. 概况 本文接着 iOS 开发之照片框架详解,侧重介绍在前文中简单介绍过的 PhotoKit 及其与 ALAssetLibrary 的差异,以及如何基于 PhotoKit 与 AlAssetLib ...

  4. iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)

    本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...

  5. iOS 开发之照片框架详解

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...

  6. iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...

  7. iOS基础 - 瀑布流

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

  8. iOS横向瀑布流的封装

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

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

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

随机推荐

  1. QT常用代码之加载动态库和弹出对话框

    作者:朱金灿 来源:http://blog.csdn.net/clever101 加载动态库的代码: typedef void (*Execute)(); // 定义导出函数类型 QString st ...

  2. CISP/CISA 每日一题 14

    CISA 每日一题(答) 自动无人值守运行(LIGHTS-OUT)优势:1.信息系统运行成本的遏制/减少:2.持续运行(24/7):3.减少系统错误和中断次数. I/O 控制人员负责保证:1.批处理信 ...

  3. canvas.toDataURL() gives “Security Error” in IE 11

    http://stackoverflow.com/questions/30101143/canvas-todataurl-gives-security-error-in-ie-11

  4. 微信支付v2开发(10) 全网发布

    关键字:微信公众平台 微信支付 全网发布 作者:方倍工作室 原文:http://www.cnblogs.com/txw1958/p/wxpay-publish.html 在这篇微信公众平台开发教程中, ...

  5. Mongodb总结6-数据库启动、停止、备份等命令

    #启动Mongodb默认启动,需要在/data/db,Windows下对应的目录是Mongod.exe所在磁盘分区的根目录,例如Mongodb存放在D:/Mongodb,那么对应的路径就是D:/dat ...

  6. WebService学习总结(5)——WebService常见开发框架比较

    在SOA领域,我们认为Web Service是SOA体系的构建单元(building block).对于服务开发人员来说,AXIS和CXF一定都不会陌生.这两个产品都是Apache孵化器下面的Web ...

  7. iOS开发RunLoop学习:一:RunLoop简单介绍

    一:RunLoop的简单介绍 #import "ViewController.h" @interface ViewController () @end @implementatio ...

  8. poj 1191 棋盘切割 (压缩dp+记忆化搜索)

    一,题意: 中文题 二.分析: 主要利用压缩dp与记忆化搜索思想 三,代码: #include <iostream> #include <stdio.h> #include & ...

  9. solrcloud集群搭建

    solrcloud 集群搭建 初始条件: 1. 三台服务器 IP 地址分别为 192.168.1.133 192.168.1.134 192.168.1.135 2. 使用 solr-5.3.1,zo ...

  10. UWP 新手教程1——UWP的前世今生

    文件夹 引言 设备族群 UI 和通用输入模式 通用控件和布局面板 工具 自适应扩展 通用输入处理 引言 在本篇文章中,可以掌握下面知识: 设备族群,怎样决定目标设备 新的UI控件和新面板帮助你适应不同 ...