第二十七篇、使用MVVM布局页面
思路:架构的设计模式主要有这么两种
>MVC :这种方式用得很多,也很是常见,不在过多的介绍
>MVVM:使用这种 常常需要导入第三方框架,常见的是响应式框架
>主要讲一下ViewModel : ViewModel 主要管理的使用是一些(不归类于Model 或者 Controller)的业务逻辑,常见的是一些网络请求,布局的一些逻辑
下面还是举个简单明了的例子好了!MVVM来了。。。 先Model(模型) @interface MyAssetsModel : NSObject@property (nonatomic,copy) NSString *typeStr;//现金余额,路费宝余额 @property (nonatomic,copy) NSString *detailStr;//现金明细,路费宝明细 @property (nonatomic,copy) NSString *money; @property (nonatomic,copy) NSString *getMoney;//余额提现,兑换现金 @property (nonatomic,copy) NSString *desc;//描述@end ViewModel的.h文件(主要用来处理一些不归类为Controller和Model,如网络请求等等) #import <Foundation/Foundation.h> #import "MyAssetsModel.h" @interface MyAssetsViewModel : NSObject@property (nonatomic, copy) NSString *cellName;@property (nonatomic, copy) NSString *cellId;@property (nonatomic, assign) CGFloat cellheight;- (instancetype)initWidthModel:(MyAssetsModel *)model;@end .m文件 @implementation MyAssetsViewModel- (instancetype)initWidthModel:(MyAssetsModel *)model {MyAssetsViewModel *viewModel = [[MyAssetsViewModel alloc] init];viewModel.cellName = @"MyAssetsTableViewCell";viewModel.cellId = @"MyAssetsTableViewCellId";viewModel.cellheight = [NSString sizeWithString:model.desc font:[UIFont systemFontOfSize:[PublicUnit CGRectMakeX:12]] ParagrapGap:[PublicUnit CGRectMakeX:6] withMaxSize:CGSizeMake(SCREEN_WIDTH- [PublicUnit CGRectMakeX:30], 2000)].height+[PublicUnit CGRectMakeX:150];return viewModel; }@end 下面是Controller(控制器) #import "FPHMyAssetsViewController.h" #import "MyAssetsTableViewCell.h" #import "MyAssetsModel.h" #import "MyAssetsViewModel.h" #import "FPHBalanceVC.h" #import "FPHDrawCashVC.h" #import "FPHRuleCashVC.h" @interface FPHMyAssetsViewController ()<UITableViewDelegate,UITableViewDataSource>@property (nonatomic,strong) UITableView *tableView; @property (nonatomic,strong) NSMutableArray *dataArray; @property (nonatomic,strong) NSMutableArray *dataViewModelArray;@end@implementation FPHMyAssetsViewController- (void)viewDidLoad {[super viewDidLoad];[self setBaseView];[self registCell]; } - (void)setBaseView {self.title = @"我的资产";[self.view addSubview:self.tableView];[self requestWalletRecord];}- (void)registCell {[self.tableView registerClass:[MyAssetsTableViewCell class] forCellReuseIdentifier:@"MyAssetsTableViewCellId"];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.dataArray.count; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {MyAssetsViewModel *viewModel = _dataViewModelArray[indexPath.row];if (!viewModel) {return nil;}if ([viewModel.cellName isEqualToString:@"MyAssetsTableViewCell"]) {MyAssetsTableViewCell *cell = (MyAssetsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:viewModel.cellId forIndexPath:indexPath];[cell cellFilledWithModel:_dataArray[indexPath.row]];cell.myAssetsBlock = ^(MyAssetsModel *model){if ([model.getMoney isEqualToString:@"余额提现"]) {[NSUserDefaults setInteger:1 forKey:RightNow];NSString *money = model.money;[NSUserDefaults setFloat:[money floatValue] forKey:CrashMoney];FPHDrawCashVC *vc = [[FPHDrawCashVC alloc] init];[[self navigationController] pushViewController:vc animated:YES];}else{[NSUserDefaults setInteger:0 forKey:RightNow];NSString *money = model.money;[NSUserDefaults setFloat:[money floatValue] forKey:CrashMoney];FPHRuleCashVC *vc = [[FPHRuleCashVC alloc] init];[self.navigationController pushViewController:vc animated:YES];}};return cell;}return nil;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {MyAssetsViewModel *viewModel = self.dataViewModelArray[indexPath.row];return viewModel.cellheight; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {if (indexPath.row == 0) {FPHBalanceVC *vc = [[FPHBalanceVC alloc] init];vc.balanceStatus = 1;[self.navigationController pushViewController:vc animated:YES];}else{FPHBalanceVC *vc = [[FPHBalanceVC alloc] init];vc.balanceStatus = 2;[self.navigationController pushViewController:vc animated:YES];} }#pragma mark -- #pragma mark -- setter and getter - (UITableView *)tableView {if (!_tableView) {_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];_tableView.delegate = self;_tableView.dataSource = self;_tableView.backgroundColor = UIColorWithHexRGB(0xf5f5f5);_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;_tableView.tableHeaderView = [self addHeader];}return _tableView; }//tableview header - (UIView *)addHeader {UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, [PublicUnit CGRectMakeX:120])];UIImageView *imgView = [[UIImageView alloc] initWithFrame:header.bounds];imgView.image = [UIImage imageNamed:@"bg_mymoney"];[header addSubview:imgView];return header; }- (NSMutableArray *)dataArray {if (!_dataArray) {_dataArray = [NSMutableArray array];MyAssetsModel *model1 = [[MyAssetsModel alloc] init];model1.typeStr =@"现金余额";model1.detailStr = @"现金明细";model1.money = @"0.00";model1.getMoney = @"余额提现";model1.desc = @"现金余额可以及时转出提现,直接转到自己的银行卡或支付宝。";[_dataArray addObject:model1];MyAssetsModel *model2 = [[MyAssetsModel alloc] init];model2.typeStr =@"路费宝余额";model2.detailStr = @"路费宝明细";model2.money = @"802.00";model2.getMoney = @"兑换现金";model2.desc = @"路费宝主要通过领路费、分享楼盘、邀请好友看房以及参与房品汇活动获得。路费宝只能兑换到现金余额,每次兑换必须是68的倍数。";[_dataArray addObject:model2];}return _dataArray; }- (NSMutableArray *)dataViewModelArray {if (!_dataViewModelArray) {_dataViewModelArray = [NSMutableArray array];for (MyAssetsModel *model in self.dataArray) {MyAssetsViewModel *viewModel = [[MyAssetsViewModel alloc] initWidthModel:model];[_dataViewModelArray addObject:viewModel];}}return _dataViewModelArray; }
转载于:https://www.cnblogs.com/HJQ2016/p/5875137.html
第二十七篇、使用MVVM布局页面相关推荐
- 第二十七篇 导航栏和内容块
导航栏和内容块 上节课留下了一个小练习: 老师做了一份,但是没有图片图标,所以代码里没有用到 img 标签. 在写之前,我们要分析吧,这个页面,难度只有左侧的导航栏.因为动态! 我们可以用到jQuer ...
- Android UI开发第二十七篇——实现左右划出菜单
年前就想写左右滑动菜单,苦于没有时间,一直拖到现在,这篇代码实现参考了网上流行的SlidingMenu,使用的FrameLayout布局,不是扩展的HorizontalScrollView. 程序中自 ...
- 单片机怎么跳出循环_自学单片机第二十七篇:矩阵按键的硬件测试
我们继续来说矩阵按键的问题,这次是用在实际电路中的.在仿真中我们没有发现问题,但是文章最后我提出了,这个电路在实际应用中一定是存在问题的,那么问题在哪里呢? 这是我们分析的电路. 前几篇我们也对硬件进 ...
- “约见”面试官系列之常见面试题第二十七篇之vue-router的重要属性
export default new Router({mode: 'history', //路由模式,取值为history与hashbase: '/', //打包路径,默认为/,可以修改routes: ...
- 第二十七篇、基于Arduino uno,获取mlx90614非接触式红外测温传感器的温度值——结果导向
0.结果 说明:先来看看串口调试助手显示的结果,第一个值是空气的温度,第二个值是被测量的物体温度,如果是你想要的,可以接着往下看. 1.外观 说明:虽然mlx90614非接触式红外测温传感器形态各异, ...
- Python之路(第二十七篇) 面向对象进阶:内置方法、描述符
一.__call__ 对象后面加括号,触发执行类下面的__call__方法. 创建对象时,对象 = 类名() :而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()( ...
- 第二十七篇 网页数据解析三种方法: 正则表达--BeautifulSoup--xpath 满满的干货
心得: 生活不允许自己懈怠自己,革命尚未成功,同志还须努力,有句话说的好,你尽管努力,剩下的交给天意. 我们从网页上抓取的原始数据大多都是html的数据格式,那如何从html中提取想要的字符串,得需要 ...
- 第二十七篇 镜像电流源
特点: 1.电流源Q2输出的电压范围很宽,可以在VCC和零点几伏之间工作: 2.可以用一个电流Ip来控制电流I: 缺点: 由于Early效应存在,在Q2中一个固定的电流,Vbe会随着集电极电压发生微小 ...
- MVC教程第二篇:第一个页面
MVC教程第二篇:第一个页面 摘要 本文首先一步一步完成Demo的第一个页面--首页.然后根据实现过程,说明一下其中用到的与ASP.NET MVC相关的概念与原理. 让第一个页面跑起来 现在, ...
最新文章
- Mysql:is not allowed to connect to this MySQL
- Spark之spark shell
- Cow Contest POJ - 3660(floyed求传递闭包)
- P1829 [国家集训队]Crash的数字表格(推了好久的mobius反演)
- Android 布局 优先显示右侧的控件
- 红外接收头图片_常见红外接收头引脚型号及测量代换
- android 结束if循环_简单探究Android平台下' if ' 语句条件判断耗时情况
- 图像质量评价(Image Quality Assessment,IQA)
- SQL server 2017安装教程
- latex中png、pdf和eps格式的图片文件转换
- 如何设计一个秒杀系统?
- LookaHead优化器
- Android 10.0 TV板 强制app横屏显示
- Java使用zip4j库 压缩文件工具类(自适应压缩包编码格式)
- 移动数据Moving Data
- 月结6 - AR和AP重分类(Regroup)[FAGLF101/OBBU/OBBV]
- 【爬虫】应用Python爬虫爬取豆瓣租房上的帖子信息
- 变分原理及Euler-Lagrange
- 在maven中使用c3p0连接池的c3p0-config.xml文件出现named-config with name ‘xxxxx‘ does not exist. Using default-con
- eclipse中html联想设置,设置eclipse联想功能