iOS開發-MVVM簡單使用(初級①)
MVC
任何一個正經開發過一陣子軟體的人都熟悉MVC,它意思是Model View Controller, 是一個在複雜應用設計中組織程式碼的公認模式. 它也被證實在 iOS 開發中有著第二種含義: Massive View Controller(重量級檢視控制器)。它讓許多程式設計師絞盡腦汁如何去使程式碼被解耦和組織地讓人滿意. 總的來說, iOS 開發者已經得出結論: 他們需要給檢視控制器瘦身, 並進一步分離事物;但該怎麼做呢?
MVVM
Model - model 在 MVVM 中沒有真正的變化. 取決於你的偏好, 你的 model 可能會或可能不會封裝一些額外的業務邏輯工作. 我更傾向於把它當做一個容納表現資料-模型物件資訊的結構體, 並在一個單獨的管理類中維護的建立/管理模型的統一邏輯。
#import <Foundation/Foundation.h> @interface ShowModel : NSObject @property (nonatomic , copy) NSString * image; @property (nonatomic , copy) NSString * begin_time; @property (nonatomic , copy) NSString * title; @property (nonatomic , copy) NSString * end_time; @end
View - view 包含實際 UI 本身(不論是 UIView 程式碼, storyboard 和 xib), 任何檢視特定的邏輯, 和對使用者輸入的反饋. 在 iOS 中這不僅需要 UIView 程式碼和那些檔案, 還包括很多需由 UIViewController 處理的工作。
#import <UIKit/UIKit.h> @class ShowModel; @interface ShowCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *iconImage; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (weak, nonatomic) IBOutlet UILabel *timeLabel; @property (weak, nonatomic) IBOutlet UILabel *addressLabel; @property (nonatomic, strong) ShowModel *model; @end
View-Model 這個術語本身會帶來困惑, 因為它混搭了兩個我們已知的術語, 但卻是完全不同的東東. 它不是傳統資料-模型結構中模型的意思(又來了, 只是我喜歡這個例子). 它的職責之一就是作為一個表現檢視顯示自身所需資料的靜態模型;但它也有收集, 解釋和轉換那些資料的責任.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@class ShowModel;
typedef void (^ReturnValueBlock) (id response);
typedef void (^ErrorBlock) (NSError *error);
@interface ShowViewModel : NSObject
/**
*資料請求
*/
- (void)getShowDataWith:(ReturnValueBlock)successBlock failMsg:(ErrorBlock)failBlock;
@end
#import "ShowViewModel.h"
#import "HttpServer.h"
@implementation ShowViewModel
-(void)getShowDataWith:(ReturnValueBlock)successBlock failMsg:(ErrorBlock)failBlock{
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"cate"] = @"latest";
[Networking enableInterfaceDebug:YES];
[Networking getWithUrl:@"https://api.douban.com/v2/onlines" params:params success:^(id response) {
successBlock(response);
NSLog(@"%@",response);
} fail:^(NSError *error) {
NSLog(@"%@",error);
failBlock(error);
}];
}
Controller 一個更加清晰明確的任務: 呈現由 view-model 提供的資料。
#import "ViewController.h"
#import "ShowCell.h"
#import "ShowViewModel.h"
#import "ShowModel.h"
#import "MJExtension.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *showArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
tableView.rowHeight = 80;
[self.view addSubview:tableView];
[tableView registerNib:[UINib nibWithNibName:@"ShowCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"show"];
ShowViewModel *viewModel = [[ShowViewModel alloc]init];
[viewModel getShowDataWith:^(id response) {
NSDictionary *dic = (NSDictionary *)response;
self.showArray = [ShowModel mj_objectArrayWithKeyValuesArray:dic[@"onlines"]];
[tableView reloadData];
} failMsg:^(NSError *error) {
}];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.showArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ShowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"show"];
cell.model = _showArray[indexPath.row];
return cell;
}
- (NSMutableArray *)showArray {
if (!_showArray) {
_showArray = [[NSMutableArray alloc] init];
}
return _showArray;
}
@end
Github地址:https://github.com/roycehe/MVVM-TableView-Demo
後續更新更多進階內容
轉載於:https://my.oschina.net/roycehe/blog/860423