一:瀑布流框架的应用:将封装好的瀑布流框架导入,遵守协议
二:代码:
1 #import "HMShopsViewController.h" 2 #import "HMShopCell.h" 3 #import "HMWaterflowView.h" 4 #import "HMShop.h" 5 #import "MJExtension.h" 6 #import "MJRefresh.h" 7 8 @interface HMShopsViewController ()<HMWaterflowViewDataSource, HMWaterflowViewDelegate> 9 @property (nonatomic, strong) NSMutableArray *shops; 10 @property (nonatomic, weak) HMWaterflowView *waterflowView; 11 @end 12 13 @implementation HMShopsViewController 14 15 - (NSMutableArray *)shops 16 { 17 if (_shops == nil) { 18 self.shops = [NSMutableArray array]; 19 } 20 return _shops; 21 } 22 23 - (void)viewDidLoad 24 { 25 [super viewDidLoad]; 26 27 // 0.初始化数据 28 NSArray *newShops = [HMShop objectArrayWithFilename:@"2.plist"]; 29 [self.shops addObjectsFromArray:newShops]; 30 31 // 1.瀑布流控件 32 HMWaterflowView *waterflowView = [[HMWaterflowView alloc] init]; 33 waterflowView.backgroundColor = [UIColor redColor]; 34 // 跟随着父控件的尺寸而自动伸缩 35 waterflowView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 36 waterflowView.frame = self.view.bounds; 37 waterflowView.dataSource = self; 38 waterflowView.delegate = self; 39 [self.view addSubview:waterflowView]; 40 self.waterflowView = waterflowView; 41 42 // 2.继承刷新控件 43 // [waterflowView addFooterWithCallback:^{ 44 // NSLog(@"进入上拉加载状态"); 45 // }]; 46 47 // [waterflowView addHeaderWithCallback:^{ 48 // NSLog(@"进入下拉加载状态"); 49 // }]; 50 51 [waterflowView addHeaderWithTarget:self action:@selector(loadNewShops)]; 52 [waterflowView addFooterWithTarget:self action:@selector(loadMoreShops)]; 53 } 54 55 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 56 { 57 // NSLog(@"屏幕旋转完毕"); 58 [self.waterflowView reloadData]; 59 } 60 61 - (void)loadNewShops 62 { 63 static dispatch_once_t onceToken; 64 dispatch_once(&onceToken, ^{ 65 // 加载1.plist 66 NSArray *newShops = [HMShop objectArrayWithFilename:@"1.plist"]; 67 [self.shops insertObjects:newShops atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, newShops.count)]]; 68 }); 69 70 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 71 // 刷新瀑布流控件 72 [self.waterflowView reloadData]; 73 74 // 停止刷新 75 [self.waterflowView headerEndRefreshing]; 76 }); 77 } 78 79 - (void)loadMoreShops 80 { 81 static dispatch_once_t onceToken; 82 dispatch_once(&onceToken, ^{ 83 // 加载3.plist 84 NSArray *newShops = [HMShop objectArrayWithFilename:@"3.plist"]; 85 [self.shops addObjectsFromArray:newShops]; 86 }); 87 88 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 89 90 // 刷新瀑布流控件 91 [self.waterflowView reloadData]; 92 93 // 停止刷新 94 [self.waterflowView footerEndRefreshing]; 95 }); 96 } 97 98 #pragma mark - 数据源方法 99 - (NSUInteger)numberOfCellsInWaterflowView:(HMWaterflowView *)waterflowView 100 { 101 return self.shops.count; 102 } 103 104 - (HMWaterflowViewCell *)waterflowView:(HMWaterflowView *)waterflowView cellAtIndex:(NSUInteger)index 105 { 106 HMShopCell *cell = [HMShopCell cellWithWaterflowView:waterflowView]; 107 108 cell.shop = self.shops[index]; 109 110 return cell; 111 } 112 113 - (NSUInteger)numberOfColumnsInWaterflowView:(HMWaterflowView *)waterflowView 114 { 115 if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) { 116 // 竖屏 117 return 3; 118 } else { 119 return 5; 120 } 121 } 122 123 #pragma mark - 代理方法 124 - (CGFloat)waterflowView:(HMWaterflowView *)waterflowView heightAtIndex:(NSUInteger)index 125 { 126 HMShop *shop = self.shops[index]; 127 // 根据cell的宽度 和 图片的宽高比 算出 cell的高度 128 return waterflowView.cellWidth * shop.h / shop.w; 129 } 130 @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的封装
1 #import "HMWaterflowViewCell.h" 2 @class HMWaterflowView, HMShop; 3 4 @interface HMShopCell : HMWaterflowViewCell 5 + (instancetype)cellWithWaterflowView:(HMWaterflowView *)waterflowView; 6 7 @property (nonatomic, strong) HMShop *shop; 8 @end
1 #import "HMShopCell.h" 2 #import "HMWaterflowView.h" 3 #import "UIImageView+WebCache.h" 4 #import "HMShop.h" 5 6 @interface HMShopCell() 7 @property (weak, nonatomic) UIImageView *imageView; 8 @property (weak, nonatomic) UILabel *priceLabel; 9 @end 10 11 @implementation HMShopCell 12 13 14 + (instancetype)cellWithWaterflowView:(HMWaterflowView *)waterflowView 15 { 16 static NSString *ID = @"SHOP"; 17 HMShopCell *cell = [waterflowView dequeueReusableCellWithIdentifier:ID]; 18 if (cell == nil) { 19 cell = [[HMShopCell alloc] init]; 20 cell.identifier = ID; 21 } 22 return cell; 23 } 24 25 - (id)initWithFrame:(CGRect)frame 26 { 27 self = [super initWithFrame:frame]; 28 if (self) { 29 30 UIImageView *imageView = [[UIImageView alloc] init]; 31 [self addSubview:imageView]; 32 self.imageView = imageView; 33 34 UILabel *priceLabel = [[UILabel alloc] init]; 35 priceLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]; 36 priceLabel.textAlignment = NSTextAlignmentCenter; 37 priceLabel.textColor = [UIColor whiteColor]; 38 [self addSubview:priceLabel]; 39 self.priceLabel = priceLabel; 40 } 41 return self; 42 } 43 44 - (void)setShop:(HMShop *)shop 45 { 46 _shop = shop; 47 48 self.priceLabel.text = shop.price; 49 [self.imageView sd_setImageWithURL:[NSURL URLWithString:shop.img] placeholderImage:[UIImage imageNamed:@"loading"]]; 50 } 51 52 - (void)layoutSubviews 53 { 54 [super layoutSubviews]; 55 56 self.imageView.frame = self.bounds; 57 58 CGFloat priceX = 0; 59 CGFloat priceH = 25; 60 CGFloat priceY = self.bounds.size.height - priceH; 61 CGFloat priceW = self.bounds.size.width; 62 self.priceLabel.frame = CGRectMake(priceX, priceY, priceW, priceH); 63 } 64 65 @end
7:model的封装
1 #import <Foundation/Foundation.h> 2 3 @interface HMShop : NSObject 4 @property (nonatomic, assign) CGFloat w; 5 @property (nonatomic, assign) CGFloat h; 6 @property (nonatomic, copy) NSString *img; 7 @property (nonatomic, copy) NSString *price; 8 @end
1 #import "HMShop.h" 2 3 @implementation HMShop 4 5 @end