MVVM
1.M:model数据的模型;
2.V:view包括view和viewController,统称view;
3.VM:放置用户验证输入逻辑,视图的显示逻辑,发起网络请求和其他各种各样的代码,说白了就是把viewController层的业务逻辑和页面逻辑等剥离出来放到viewmodel层;
以tableView为例
model 层声明
#import <Foundation/Foundation.h>
@interface CustomModel : NSObject
@property (nonatomic,strong) NSString *title;
@end
#import "CustomModel.h"
@implementation CustomModel
@end
V层创建tableview添加上拉加载和下拉刷新
#import "TableViewController.h"
#import "YiRefreshHeader.h"
#import "YiRefreshFooter.h"
#import "TableViewModel.h"
#import "TableViewDataSource.h"
#import "TableViewDelegate.h"
@interface TableViewController ()
{
YiRefreshHeader *refreshHeader;
YiRefreshFooter *refreshFooter;
NSMutableArray *totalSource;
TableViewModel *tableViewModel;
UITableView *tableView;
TableViewDataSource *tableViewDataSource;
TableViewDelegate *tableViewDelegate;
}
@end
@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];
tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, WScreen, HScreen-64) style:UITableViewStylePlain];
[self.view addSubview:tableView];
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];
};
// 是否在进入该界面的时候就开始进入刷新状态
[refreshHeader beginRefreshing];
// YiRefreshFooter 底部刷新按钮的使用
refreshFooter=[[YiRefreshFooter alloc] init];
refreshFooter.scrollView=tableView;
[refreshFooter footer];
refreshFooter.beginRefreshingBlock=^(){
__strong typeof(self) strongSelf = weakSelf;
[strongSelf footerRefreshAction];
};
}
- (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];
}];
}
V遵守UITableViewDataSource,UITableViewDelegate协议,并实现代理方法
#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
#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
VM层加载处理数据,
#import <Foundation/Foundation.h>
typedef void (^callback) (NSArray *array);
@interface TableViewModel : NSObject
//tableView头部刷新的网络请求
- (void)headerRefreshRequestWithCallback:(callback)callback;
//tableView底部刷新的网络请求
- (void)footerRefreshRequestWithCallback:(callback)callback;
@end
#import "TableViewModel.h"
#import "CustomModel.h"
@interface TableViewModel ()
@end
@implementation TableViewModel
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
- (void)headerRefreshRequestWithCallback:(callback)callback
{
// 后台执行:
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
{
// 后台执行:
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);
});
});
}
@end