MVVM架構簡單使用

http://blog.cocoachina.com/article/34636#st-1

項目github地址 https://github.com/zhangjiahuan8888/mvvmDemo/tree/master

開篇

MVC Model-View-Controller是一個用來組織代碼的權威範式。Apple甚至是這麼說的。在MVC下,所有的對象被歸類爲一個model,一個view,或一個controller。Model持有數據,View顯示與用戶交互的界面,而View Controller調解Model和View之間的交互。 MVVM MVVM的出現主要是爲了解決在開發過程中Controller越來越龐大的問題,變得難以維護,所以MVVM把數據加工的任務從Controller中解放了出來,使得Controller只需要專注於數據調配的工作,ViewModel則去負責數據加工並通過通知機制讓View響應ViewModel的改變。


效果圖

qadsdasdasdas.png

源碼分析

1.Model模塊

首先是model層的代碼,創建MovieModel類,繼承自NSObject。設置幾個屬性movieName、year、imageUrl、detailUrl(用於跳轉)

#import <Foundation>

@interface MovieModel : NSObject
@property (strong, nonatomic) NSString *movieName;
@property (strong, nonatomic) NSString *year;
@property (strong, nonatomic) NSURL *imageUrl;
@property (strong, nonatomic) NSString *detailUrl;
@end
ViewController模塊

ViewController裏面是用來從MovieViewModel中獲取數據信息然後展示到UITableView的cell上面,和點擊單元格的頁面跳轉

#import "HomeViewController.h"#import "MovieViewModel.h"#import "MovieCell.h"@interface HomeViewController ()<UITableViewDataSource>@property (nonatomic,strong) NSArray *modelArray;@end@implementation HomeViewController- (void)viewDidLoad { [super viewDidLoad]; self.title = @"電影首頁"; self.view.backgroundColor = [UIColor redColor]; UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; tableView.dataSource = self; tableView.delegate = self; tableView.rowHeight = 80; [self.view addSubview:tableView]; [tableView registerClass:[MovieCell class] forCellReuseIdentifier:@"MovieCell"]; //初始化MovieViewModel,設置成功(returnBlock)和失敗的回調(errorBlock),getMovieData去請求數據,請求數據成功即回調上一步設置的returnBlock,請求失敗則回調errorBlock MovieViewModel *viewModel = [[MovieViewModel alloc] init]; viewModel.returnBlock = ^(id returnValue){ _modelArray = returnValue; [tableView reloadData]; }; viewModel.errorBlock = ^(id errorCode){ NSLog(@"%@",errorCode); }; [viewModel getMovieData];}#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _modelArray.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MovieCell"]; cell.model = _modelArray[indexPath.row]; return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ MovieViewModel *movieViewModel = [[MovieViewModel alloc] init]; [movieViewModel movieDetailWithPublicModel:_modelArray[indexPath.row] WithViewController:self];}@end

View模塊

view這裏跟MVC中子類化UITableViewCell一樣,將model數據傳進來,複寫setModel方法給視圖傳遞數據

#import "MovieModel.h"
@interface MovieCell : UITableViewCell
@property (nonatomic,strong) MovieModel *model;
@end

#import "MovieCell.h"
#import "UIImageView+AFNetworking.h"
@interface MovieCell()
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UILabel *yearLabel;
@property (nonatomic,strong) UIImageView *imgView;
@end
@implementation MovieCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, 40, 60)];
        [self.contentView addSubview:_imgView];

        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, 200, 20)];
        [self.contentView addSubview:_nameLabel];

        _yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 50, 100, 20)];
        _yearLabel.textColor = [UIColor lightGrayColor];
        _yearLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_yearLabel];
    }
    return self;
}

- (void)setModel:(MovieModel *)model{
    _model = model;
    _nameLabel.text = _model.movieName;
    _yearLabel.text = _model.year;
    [_imgView setImageWithURL:_model.imageUrl];
}

@end
ViewModel模塊

ViewModel是最重要的模塊: 1.這裏面進行了數據的網絡請求,並將數據轉換成model放在一個數組中,調用_returnBlock將model數組傳遞給viewController進行數據源的處理 2.將頁面跳轉的邏輯放在ViewModel中,點擊單元格進行頁面跳轉的時候直接在- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath中調用MovieViewModel的該方法即可

#import <Foundation>#import "MovieModel.h"#import <UIKit>typedef void (^ReturnValueBlock) (id returnValue);typedef void (^ErrorCodeBlock) (id errorCode);@interface MovieViewModel : NSObject@property (nonatomic,copy) ReturnValueBlock returnBlock;@property (nonatomic,copy) ErrorCodeBlock errorBlock;//獲取電影數據- (void)getMovieData;//跳轉到電影詳情頁- (void)movieDetailWithPublicModel: (MovieModel *)movieModel WithViewController: (UIViewController *)superController;@end#import "MovieViewModel.h"#import "NetworkService.h"#import "MovieModel.h"#import "MovieViewController.h"@implementation MovieViewModel- (void)getMovieData{ [NetworkService requestWithURL:@"/v2/movie/coming_soon" params:nil success:^(id result) { NSLog(@"%@",result); NSArray *subjects = result[@"subjects"]; NSMutableArray *modelArr = [NSMutableArray arrayWithCapacity:subjects.count]; for (NSDictionary *subject in subjects) { MovieModel *model = [[MovieModel alloc] init]; model.movieName = subject[@"title"]; model.year = subject[@"year"]; NSString *urlStr = subject[@"images"][@"medium"]; model.imageUrl = [NSURL URLWithString:urlStr]; model.detailUrl = subject[@"alt"]; [modelArr addObject:model]; } _returnBlock(modelArr); } failure:^(NSError *error) { NSLog(@"%@",error); _errorBlock(error); }];}- (void)movieDetailWithPublicModel: (MovieModel *)movieModel WithViewController: (UIViewController *)superController{ MovieViewController *movieVC = [[MovieViewController alloc] init]; movieVC.url = movieModel.detailUrl; [superController.navigationController pushViewController:movieVC animated:YES];}@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章