第二十七篇、使用MVVM布局页面
生活随笔
收集整理的這篇文章主要介紹了
第二十七篇、使用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布局页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 20150905-Y1506401-19
- 下一篇: 关于C语言中运算符优先级的一次错误