ios开发瀑布流框架的应用
一:瀑布流框架的应用:将封装好的瀑布流框架导入,遵守协议
二:代码:
#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开发瀑布流框架的应用的更多相关文章
- ios开发瀑布流框架的封装
一:瀑布流框架封装的实现思路:此瀑布流框架的封装仿照tableView的底层实现,1:每个cell的frame的设置都是找出每列的最大y值,比较每列的最大y值,将下一个cell放在最大y值最小的那一列 ...
- iOS开发瀑布流的实现
//自定义布局,继承于UICollectionViewLayout #import <UIKit/UIKit.h> @class WaterFlowLayout; @protocol W ...
- iOS 开发之照片框架详解(2)
一. 概况 本文接着 iOS 开发之照片框架详解,侧重介绍在前文中简单介绍过的 PhotoKit 及其与 ALAssetLibrary 的差异,以及如何基于 PhotoKit 与 AlAssetLib ...
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)
本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...
- iOS 开发之照片框架详解
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...
- iOS基础 - 瀑布流
一.瀑布流简介 瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pint ...
- iOS横向瀑布流的封装
前段时间, 做一个羡慕, 需要使用到瀑布流! 说道瀑布流, 或许大家都不陌生, 瀑布流的实现也有很多种! 从scrollView 到 tableView 书写的瀑布流, 然后再到2012年iOS6 苹 ...
- iOS - UICollectionView 瀑布流 添加表头视图的坑
UICollectionView 瀑布流 添加表头视图的坑 首先是,需求加了个头视图在顶部,在collectionView中的头视图跟TableView的不一样,TableView的表头只要设置tab ...
随机推荐
- Bag of Features (BOF)图像检索算法
1.首先.我们用surf算法生成图像库中每幅图的特征点及描写叙述符. 2.再用k-means算法对图像库中的特征点进行训练,生成类心. 3.生成每幅图像的BOF.详细方法为:推断图像的每一个特征点与哪 ...
- [CortexM0--stm32f0308]discovery开发板
问题描写叙述:stm32提供了很多IC入门级开发板,价格还是蛮廉价的. stm32f0308-discovery就是一款cortex-m0架构的入门级开发板. 例如以下对其进行下简介. IO便 ...
- javascript之Ajax起步
XMLHttpRequest readyState属性的值: UNSENT--0--已创建XMLHttpRequest对象. OPENED--1--已调用open方法: HEADERS_RECEIV ...
- Flume Channel Selectors官网剖析(博主推荐)
不多说,直接上干货! Flume Sources官网剖析(博主推荐) Flume Channels官网剖析(博主推荐) 一切来源于flume官网 http://flume.apache.org/Flu ...
- ajax的get请求与编码
window.onload = function(){ document.getElementById('username').onblur = function(){ var name = docu ...
- ASM学习笔记--ASM 4 user guide 第一章翻译
ASM是什么? 借用别人的话 :ASM 是一个 Java 字节码操控框架.它能被用来动态生成类或者增强既有类的功能. ASM 可以直接产生二进制 class 文件,也可以在类被加载入 Java 虚拟机 ...
- WebClient HttpWebRequest从网页中获取请求数据
WebClient HttpWebRequest //HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(urlAddress) ...
- Android Material风格的应用(五)--CollapsingToolbar
Collapsing Toolbar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewA ...
- js进阶 13-8 jquery如何实现侧边栏
js进阶 13-8 jquery如何实现侧边栏 一.总结 一句话总结:先是把侧边栏设置为left为-100px,隐藏起来,jquery自定义动画animate里面的改变元素的距左边的宽度left,sl ...
- 什么是MVC,什么是WCF
在C#中总会遇到这几个概念,网上搜了一下,做一下总结和比较,东拼西凑,如有雷同,纯属直接拷贝,人懒,但无意侵权. 1.什么是MVC MVC是三个单词的首字母缩写,它们是Model(模型).View(视 ...