- 现在个人项目中大多代码都是MVC的,少数MVVM代码。静下来看看之前的代码,有时候真的不好维护和查找之前的逻辑,最近闲下来了。好好学习一下MVVM。写了点笔记,需要的就看看。
- 各种理论我就不多说了,看了很多文章。需要的自己查资料吧。
Model
// model.h
@interface DataModel : NSObject
@property (nonatomic,strong) NSString *nameStr;
@property (nonatomic,strong) NSString *phoneNum;
- (instancetype)initWithNameStr:(NSString *)name phoneNum:(NSString *)phone;
// model.m
- (instancetype)initWithNameStr:(NSString *)name phoneNum:(NSString *)phone{
self = [super init];
if (self){
_nameStr = name;
_phoneNum = phone;
}
return self;
}
@end
viewModel
@class DataViewModel;
@protocol DataViewModelDelegate<NSObject>
- (void)reloadDataWithViewModel:(DataViewModel *)viewModel;
@end
@interface DataViewModel : NSObject
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,weak) id<DataViewModelDelegate> delegate;
- (instancetype)initWithDataModelArr:(NSArray *)modelArr;
- (void)refreshData;
- (void)addUserInfo;
- (void)addUserInfoWithNameStr:(NSString *)name phoneNum:(NSString *)num;
// viewmodel.m
- (instancetype)init{
self = [super init];
if (self){
self.dataArr = [NSMutableArray new];
[self refreshData];
}
return self;
}
- (void)refreshData{
DataModel *model1 = [[DataModel alloc] initWithNameStr:@"涛胖子" phoneNum:@"182****6119"];
DataModel *model2 = [[DataModel alloc] initWithNameStr:@"产品龙" phoneNum:@"186****4778"];
DataModel *model3 = [[DataModel alloc] initWithNameStr:@"测试雨" phoneNum:@"180****1483"];
DataModel *model4 = [[DataModel alloc] initWithNameStr:@"经理远" phoneNum:@"138****7279"];
// 这里模仿网络请求拿数据,需要什么参数就通过控制器传过来
self.dataArr = [@[model1,model2,model3,model4] mutableCopy];
if (self.delegate && [self.delegate respondsToSelector:@selector(reloadDataWithViewModel:)]){
[self.delegate reloadDataWithViewModel:self];
}
}
- (void)addUserInfo{
DataModel *model1 = [[DataModel alloc] initWithNameStr:@"涛胖子" phoneNum:@"182****6119"];
[self.dataArr addObject:model1];
if (self.delegate && [self.delegate respondsToSelector:@selector(reloadDataWithViewModel:)]){
[self.delegate reloadDataWithViewModel:self];
}
}
- (void)addUserInfoWithNameStr:(NSString *)name phoneNum:(NSString *)num{
DataModel *model1 = [[DataModel alloc] initWithNameStr:name phoneNum:num];
[self.dataArr addObject:model1];
if (self.delegate && [self.delegate respondsToSelector:@selector(reloadDataWithViewModel:)]){
[self.delegate reloadDataWithViewModel:self];
}
}
view
// view.h
@class DataViewModel;
@interface TestTableView : UITableView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;
@property (nonatomic,strong) DataViewModel *viewModel;
// view.m
#import "TestTableView.h"
#import "DataViewModel.h"
#import "DataModel.h"
@interface TestTableView()<UITableViewDelegate,UITableViewDataSource,DataViewModelDelegate>
@end
@implementation TestTableView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
self = [super initWithFrame:frame style:style];
if (self){
self.delegate = self;
self.dataSource = self;
self.viewModel = [[DataViewModel alloc] init];
self.viewModel.delegate = self;
}
return self;
}
#pragma mark -- DataViewModelDelegate
- (void)reloadDataWithViewModel:(DataViewModel *)viewModel{
[self reloadData];
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.viewModel.dataArr) {
return self.viewModel.dataArr.count;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [UITableViewCell new];
if (self.viewModel.dataArr) {
cell.textLabel.text = ((DataModel*)self.viewModel.dataArr[indexPath.row]).nameStr;
cell.detailTextLabel.text = ((DataModel*)self.viewModel.dataArr[indexPath.row]).phoneNum;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *numStr = [NSString stringWithFormat:@"num: %@",((DataModel*)self.viewModel.dataArr[indexPath.row]).phoneNum];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"他的电话号码是:" message: numStr preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alert animated:YES completion:nil];
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[self.viewModel.dataArr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}];
return @[deleteAction];
}
@end
控制器中代码
@property (nonatomic,strong) TestTableView *tableView;
@property (nonatomic,strong) DataViewModel *dataViewModel;
@end
@implementation TestMVVMCtrl
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleDone target:self action:@selector(addUser)];
[self setUI];
}
- (void)addUser{
// [self.tableView.viewModel addUserInfo];
self.dataViewModel = self.tableView.viewModel;
[self.dataViewModel addUserInfoWithNameStr:@"张三" phoneNum:@"12345678989"];
}
- (void)setUI{
self.tableView = [[TestTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- 这样做的目的是减少控制器中的代码。当然其实写了更多的代码。但是代码就很清晰了,简单界面就算了。那种比较复杂的界面还是可以用一下的。代码很舒服。简单的MVVM就完成了,接下来就是用在项目中和结合RAC 来用了。
---来自涛胖子的工作笔记