由于之前項(xiàng)目使用的一直是MVC模式秧廉,所以一直也沒有過多的去了解MVVM模式荧库。換了一家新公司后膛壹,接手的項(xiàng)目中完全使用MVVM結(jié)構(gòu)驾中。所以就老老實(shí)實(shí)的學(xué)習(xí)了MVVM設(shè)計(jì)模式。下面談?wù)勈菍?duì)MVVM的見解模聋。
首先看一下MVVM在項(xiàng)目中的目錄結(jié)構(gòu)(如下圖)
其實(shí)MVVM是MVC模式的一種衍生肩民,MVVM設(shè)計(jì)模式巧用了viewModel將原本在控制器(viewController)中和model的復(fù)雜邏輯抽取出來,將這些邏輯放到viewModel中链方,使得控制器變得更加輕巧持痰,層次結(jié)構(gòu)更加清晰。對(duì)比下MVVM和MVC在實(shí)際項(xiàng)目中的區(qū)別祟蚀。(如下圖)
簡(jiǎn)單的對(duì)比下是不是發(fā)現(xiàn)MVVM設(shè)計(jì)模式下控制器中的代碼更加的簡(jiǎn)潔清爽工窍。
MVVM分別指什么
Model-數(shù)據(jù)層
ViewController/View-展示層
ViewModel- 數(shù)據(jù)模型 :ViewModel層,就是View和Model層的粘合劑前酿,他是一個(gè)放置用戶輸入驗(yàn)證邏輯患雏,視圖顯示邏輯,發(fā)起網(wǎng)絡(luò)請(qǐng)求和其他各種各樣的代碼的極好的地方罢维。說白了淹仑,就是把原來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的對(duì)應(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ò)獲取到的匀借,這里模擬給出一個(gè)數(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獲取對(duì)應(yīng)分區(qū)的行數(shù)
return [self.cellViewModel numberOfItemsInSection:section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ //通過viewModel獲取對(duì)應(yīng)分區(qū)數(shù)
return [self.cellViewModel numberOfSections];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [self.cellViewModel tableView:tableView heightForRowAtIndexPath:indexPath];
}
@end