MVVM學(xué)習(xí)心得

由于之前項目使用的一直是MVC模式,所以一直也沒有過多的去了解MVVM模式。換了一家新公司后,接手的項目中完全使用MVVM結(jié)構(gòu)。所以就老老實(shí)實(shí)的學(xué)習(xí)了MVVM設(shè)計模式。下面談?wù)勈菍VVM的見解。
首先看一下MVVM在項目中的目錄結(jié)構(gòu)(如下圖)


屏幕快照 2017-08-11 下午4.46.13.png

其實(shí)MVVM是MVC模式的一種衍生,MVVM設(shè)計模式巧用了viewModel將原本在控制器(viewController)中和model的復(fù)雜邏輯抽取出來,將這些邏輯放到viewModel中,使得控制器變得更加輕巧,層次結(jié)構(gòu)更加清晰。對比下MVVM和MVC在實(shí)際項目中的區(qū)別。(如下圖)

MVC在控制器中的代碼
MVVM模式下控制器中的代碼

簡單的對比下是不是發(fā)現(xiàn)MVVM設(shè)計模式下控制器中的代碼更加的簡潔清爽。

MVVM分別指什么
Model-數(shù)據(jù)層
ViewController/View-展示層
ViewModel- 數(shù)據(jù)模型 :ViewModel層,就是View和Model層的粘合劑,他是一個放置用戶輸入驗證邏輯,視圖顯示邏輯,發(fā)起網(wǎng)絡(luò)請求和其他各種各樣的代碼的極好的地方。說白了,就是把原來ViewController層的業(yè)務(wù)邏輯和頁面邏輯等剝離出來放到ViewModel層:他的任務(wù)就是從ViewModel層獲取數(shù)據(jù),然后顯示。

具體實(shí)現(xiàn)如下:
Model類:

@interface FDDCellModel : NSObject

@property (nonatomic, copy) NSString *image;
@property (nonatomic, copy) NSString *title;
- (id)initWithDictionary:(NSDictionary *)dict;
+ (id)FDDInfoWithDictionary:(NSDictionary *)dict;

@end
#import "FDDCellModel.h"

@implementation FDDCellModel

- (id)initWithDictionary:(NSDictionary *)dict{

    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
    }

    return self;
}
+ (id)FDDInfoWithDictionary:(NSDictionary *)dict{
    return [[self alloc] initWithDictionary:dict];
}
@end

View類(tableViewCell)

@class FDDCellModel;
@interface FDDTableViewCell : UITableViewCell
@property(nonatomic,strong)FDDCellModel *model;
+ (instancetype)cellWIthTableView:(UITableView *)tableView;
@end

#import "FDDCellModel.h"
@interface FDDTableViewCell()

@property (weak, nonatomic) IBOutlet UIImageView *leftImage;
@property (weak, nonatomic) IBOutlet UILabel *title;
@end

@implementation FDDTableViewCell
+ (instancetype)cellWIthTableView:(UITableView *)tableview {
    static NSString *cellID = @"FDDTableViewCell";
    FDDTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = (FDDTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"FDDTableViewCell" owner:self options:nil] lastObject];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    return cell;
}

- (void)setModel:(FDDCellModel *)model{//通過model給控件賦值
    _model = model;
    _leftImage.image = [UIImage imageNamed:_model.image];
    _title.text = _model.title;
}

viewModel類:

@class FDDTableViewCell, UITableView;
@interface FDDCellViewModel : NSObject

@property (nonatomic, strong) NSMutableArray *FDDInfoArray;
//返回tableView的分區(qū)數(shù)
- (NSInteger)numberOfSections;
//返回tableView的對應(yīng)分區(qū)的行數(shù)
- (NSInteger)numberOfItemsInSection:(NSInteger)section;
//返回自定義cell
- (FDDTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
//tableView點(diǎn)擊方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//返回tableView的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
@end

#import "FDDCellViewModel.h"
#import "FDDCellModel.h"
#import "FDDTableViewCell.h"
@interface FDDCellViewModel()

@end

@implementation FDDCellViewModel

- (NSMutableArray *)FDDInfoArray{
    if (_FDDInfoArray == nil) {
        _FDDInfoArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _FDDInfoArray;
}


- (instancetype)init{
    self = [super init];

    if (self) {
        [self loadData];
    }
    return self;
}

- (void)loadData{
    //實(shí)際開發(fā)數(shù)據(jù)是網(wǎng)絡(luò)獲取到的,這里模擬給出一個數(shù)據(jù)
    NSArray *array = @[
                       @{@"image" : @"leon", @"title" : @"標(biāo)題1"},
                        @{@"image" : @"leon", @"title" : @"標(biāo)題2"},
                       @{@"image" : @"leon", @"title" : @"標(biāo)題3"},
                         @{@"image" : @"leon", @"title" : @"標(biāo)題4"},
                         @{@"image" : @"leon", @"title" : @"標(biāo)題5"},
                         @{@"image" : @"leon", @"title" : @"標(biāo)題6"},
                         @{@"image" : @"leon", @"title" : @"標(biāo)題7"}
                       ];
    for (NSDictionary *dict in array) {
        FDDCellModel *model = [FDDCellModel FDDInfoWithDictionary:dict];
        [self.FDDInfoArray addObject:model];

    }
}

- (NSInteger)numberOfSections{
    return 1;
}
- (NSInteger)numberOfItemsInSection:(NSInteger)section{

    return self.FDDInfoArray.count;
}
- (FDDTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FDDTableViewCell *cell = [FDDTableViewCell cellWIthTableView:tableView];
    cell.model = self.FDDInfoArray[indexPath.row];

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
@end

controller類:

@interface FDDTableViewController ()

@property(nonatomic,strong)FDDCellViewModel *cellViewModel;

@end

@implementation FDDTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"MVVM simpleOne";
    self.cellViewModel = [[FDDCellViewModel alloc] init];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  //通過viewModel獲取自定義cell
  return   [self.cellViewModel tableView:tableView cellForRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  //通過viewModel獲取對應(yīng)分區(qū)的行數(shù)
    return [self.cellViewModel numberOfItemsInSection:section];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{  //通過viewModel獲取對應(yīng)分區(qū)數(shù)
    return [self.cellViewModel numberOfSections];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   return  [self.cellViewModel tableView:tableView heightForRowAtIndexPath:indexPath];
}

@end

具體demo鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容