MVVM初體驗(yàn)

  • 現(xiàn)在個(gè)人項(xiàng)目中大多代碼都是MVC的,少數(shù)MVVM代碼。靜下來(lái)看看之前的代碼,有時(shí)候真的不好維護(hù)和查找之前的邏輯,最近閑下來(lái)了。好好學(xué)習(xí)一下MVVM。寫(xiě)了點(diǎn)筆記,需要的就看看。
  • 各種理論我就不多說(shuō)了,看了很多文章。需要的自己查資料吧。

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:@"產(chǎn)品龍" phoneNum:@"186****4778"];
    DataModel *model3 = [[DataModel alloc] initWithNameStr:@"測(cè)試雨" phoneNum:@"180****1483"];
    DataModel *model4 = [[DataModel alloc] initWithNameStr:@"經(jīng)理遠(yuǎn)" phoneNum:@"138****7279"];
    // 這里模仿網(wǎng)絡(luò)請(qǐng)求拿數(shù)據(jù),需要什么參數(shù)就通過(guò)控制器傳過(guò)來(lái)
    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:@"他的電話(huà)號(hào)碼是:" 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];

}
  • 這樣做的目的是減少控制器中的代碼。當(dāng)然其實(shí)寫(xiě)了更多的代碼。但是代碼就很清晰了,簡(jiǎn)單界面就算了。那種比較復(fù)雜的界面還是可以用一下的。代碼很舒服。簡(jiǎn)單的MVVM就完成了,接下來(lái)就是用在項(xiàng)目中和結(jié)合RAC 來(lái)用了。

---來(lái)自濤胖子的工作筆記

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容