首页 > 代码库 > 第二十七篇、使用MVVM布局页面
第二十七篇、使用MVVM布局页面
下面还是举个简单明了的例子好了!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;//描述@endViewModel的.h文件#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;}文/Migi000(简书作者)原文链接:http://www.jianshu.com/p/51bf38835461著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
第二十七篇、使用MVVM布局页面
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。