1) model
- model 和MVC 的model 一模一樣
2) View --- TableViewCell
- TableViewCell 和MVC 的TableViewCell 一模一樣
.h
#import <UIKit/UIKit.h>
@interface CustomTableViewCell : UITableViewCell
@property(nonatomic,strong) UILabel *titleLabel;
@end
.m
#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, WScreen, 50)];
[self.contentView addSubview:_titleLabel];
_titleLabel.backgroundColor=[UIColor whiteColor];
_titleLabel.font=[UIFont systemFontOfSize:14];
}
return self;
}
3) controller - TableViewController和MVC的Controller基本一樣,會變精簡.
因為
- UIViewcontroller的部分邏輯挪動到ViewModel和protocal 中了
- 我可以簡單認為:MVC中Controller == MVVM中的Controller+ViewModel+Protocol
@implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if (iOS7) {
self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
}
self.title=@"MVVMDemo With TableView";
self.view.backgroundColor=[UIColor whiteColor];
//創(chuàng)建tableView
tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, WScreen, HScreen-64) style:UITableViewStylePlain];
[self.view addSubview:tableView];
//設置數(shù)據(jù)源代理
tableViewDataSource=[[TableViewDataSource alloc] init];
tableViewDelegate=[[TableViewDelegate alloc] init];
tableView.dataSource=tableViewDataSource;
tableView.delegate=tableViewDelegate;
tableViewModel=[[TableViewModel alloc] init];
totalSource=0;
// YiRefreshHeader 頭部刷新按鈕的使用
refreshHeader=[[YiRefreshHeader alloc] init];
refreshHeader.scrollView=tableView;
[refreshHeader header];
__weak typeof(self) weakSelf = self;
refreshHeader.beginRefreshingBlock=^(){
__strong typeof(self) strongSelf = weakSelf;
[strongSelf headerRefreshAction];
};
// 是否在進入該界面的時候就開始進入刷新狀態(tài)
[refreshHeader beginRefreshing];
// YiRefreshFooter 底部刷新按鈕的使用
refreshFooter=[[YiRefreshFooter alloc] init];
refreshFooter.scrollView=tableView;
[refreshFooter footer];
refreshFooter.beginRefreshingBlock=^(){
__strong typeof(self) strongSelf = weakSelf;
[strongSelf footerRefreshAction];
};
}
4) ViewModel 實現(xiàn)加載數(shù)據(jù)請求和刷新.(MVVM獨有的)
- (void)headerRefreshAction
{
[tableViewModel headerRefreshRequestWithCallback:^(NSArray *array){
totalSource=(NSMutableArray *)array;
tableViewDataSource.array=totalSource;
tableViewDelegate.array=totalSource;
[refreshHeader endRefreshing];
[tableView reloadData];
}];
}
- (void)footerRefreshAction
{
[tableViewModel footerRefreshRequestWithCallback:^(NSArray *array){
[totalSource addObjectsFromArray:array] ;
tableViewDataSource.array=totalSource;
tableViewDelegate.array=totalSource;
[refreshFooter endRefreshing];
[tableView reloadData];
}];
}
@end
- (void)headerRefreshRequestWithCallback:(callback)callback
{
// 后臺執(zhí)行:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(2);
dispatch_async(dispatch_get_main_queue(), ^{
// 主線程刷新視圖
NSMutableArray *arr=[NSMutableArray array];
for (int i=0; i<16; i++) {
int x = arc4random() % 100;
NSString *string=[NSString stringWithFormat:@" (random%d) 君子性非異也,善假于物也!",x];
CustomModel *model=[[CustomModel alloc] init];
model.title=string;
[arr addObject:model];
}
callback(arr);
});
});
}
- (void )footerRefreshRequestWithCallback:(callback)callback
{
// 后臺執(zhí)行:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(2);
dispatch_async(dispatch_get_main_queue(), ^{
// 主線程刷新視圖
NSMutableArray *arr=[NSMutableArray array];
for (int i=0; i<16; i++) {
int x = arc4random() % 100;
NSString *string=[NSString stringWithFormat:@" (random%d) 君子性非異也,善假于物也!",x];
CustomModel *model=[[CustomModel alloc] init];
model.title=string;
[arr addObject:model];
}
callback(arr);
});
});
}
5) TableViewProtocol (MVVM獨有的)
** TableViewDataSource和MVC 的協(xié)議一樣只是抽取出來而已**
#import "TableViewDataSource.h"
#import "CustomTableViewCell.h"
@implementation TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.titleLabel.text=((CustomModel *)[_array objectAtIndex:indexPath.row]).title;
return cell;
}
@end
** TableViewDelegate代理,和MVC一樣只是把他抽取出來而已**
#import "TableViewDelegate.h"
@implementation TableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_array.count>0) {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:((CustomModel *)[_array objectAtIndex:indexPath.row]).title delegate:nil cancelButtonTitle:@"sure" otherButtonTitles:nil, nil];
[alert show];
}
}
@end
- 作者其他文章推薦,持續(xù)更新學習心得筆記
成為iOS頂尖高手,你必須來這里(這里有最好的開源項目和文章)