第二十七篇、使用MVVM布局页面

时间:2023-12-23 11:49:50

第二十七篇、使用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:]] ParagrapGap:[PublicUnit CGRectMakeX:] withMaxSize:CGSizeMake(SCREEN_WIDTH- [PublicUnit CGRectMakeX:], )].height+[PublicUnit CGRectMakeX:]; 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: 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: 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 == ) {
FPHBalanceVC *vc = [[FPHBalanceVC alloc] init];
vc.balanceStatus = ;
[self.navigationController pushViewController:vc animated:YES];
}else{ FPHBalanceVC *vc = [[FPHBalanceVC alloc] init];
vc.balanceStatus = ;
[self.navigationController pushViewController:vc animated:YES]; }
} #pragma mark --
#pragma mark -- setter and getter
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , 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(, , SCREEN_WIDTH, [PublicUnit CGRectMakeX:])];
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;
}