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

二:代码:

 #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

最新文章

  1. java获取json格式中的值
  2. PHP数组处理函数的使用array_map(三)
  3. SSH 小总
  4. JDE FORM开发--checkBox
  5. PHP和Golang使用Thrift1和Thrift2访问Hbase0.96.2(ubuntu12.04)
  6. Jquery 操作页面中iframe自动跟随窗口大小变化,而页面不出现滚动条,只在iframe内部出滚动条
  7. ubuntu 设置root启动
  8. Java中日期时间API小结
  9. ThinkPHP - 独立分组项目搭建
  10. Ehcache入门经典:第二篇ehcache.xml的参数
  11. idea打开dashboard
  12. jQuery-animate万能动画效果
  13. github咋用昂
  14. dropdownlist 绑定方法
  15. 1.1 Java 的概述
  16. bzoj2938(ac自动机)
  17. 如鹏网学习笔记(十三)EasyUI
  18. resultAPI示例
  19. java中TreeMap集合的常用方法
  20. Raspberry Pi使用

热门文章

  1. 可重入锁ReentrantLock--转载
  2. 二维LIS(CDQ分治)
  3. Direct2D 如何关闭抗锯齿
  4. thinkphp 整合 swiftmailer 实现邮件发送
  5. bootstrap课程7 jquery中结束之前动画用什么
  6. html只能有一个id,并且id的值只能是一个
  7. Altium Designer中的粉红色网格和绿色框框
  8. 将一个类写成WebService服务的形式
  9. MWPhotoBrowser 属性详解 和代理解释
  10. Surging 微服务框架使用入门