簡單的嘗試一下MVVM + RAC 休雌,這個東西用過的都說好址芯。 VC 控制在200行不是夢哥捕。 啟動的VC不用管要销,直接點擊屏幕進第二個控制器就好
https://git.oschina.net/wyChirs/MVVM-RAC.git
MVVM.gif
1.先創(chuàng)建好VC ViewModel View 各自的類
我把UITableView 頁抽出來构回,自定義了一個。外加一個HeadView疏咐。主要完成數(shù)據(jù)的綁定纤掸,加載,更新(MJRefresh)
在UITableView的初始化方法中 設(shè)置代理浑塞,上下拉刷新等
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
self = [super initWithFrame:frame style:style];
if (self) {
self.dataArray = [NSMutableArray array];
self.page = 1;
self.delegate = self;
self.dataSource = self;
self.tableHeaderView = self.headView;
self.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
self.mj_footer.automaticallyHidden = YES;
}
return self;
}
上下拉刷新的方法(控制分頁)
-(void)refresh{
self.page = 1;
//該方法是執(zhí)行 refreshCommand 事件 @[self,@(self.page)] 傳遞的參數(shù) 執(zhí)行該方法后 會觸發(fā)VM 的 refreshCommand 事件借跪, 并且在取得參數(shù)時是按照數(shù)組下標(biāo)對應(yīng)獲取
[self.viewModel.refreshCommand execute:@[self,@(self.page)]];
}
-(void)loadMoreData{
self.page++;
[self.viewModel.refreshCommand execute:@[self,@(self.page)]];
}
在自定義的TableView中 Cell 的點擊事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// 執(zhí)行 itemClickCommand 傳入?yún)?shù)
[self.viewModel.itemClickCommand execute:@[[NSNumber numberWithInteger:indexPath.row],self.headView]];
}
2.最主要的就是 VM的創(chuàng)建
首先確定VM 需要做哪幾件事情 :
- 1 網(wǎng)絡(luò)請求 數(shù)據(jù)更新
- 2 處理UItableviewCell 的點擊事件
聲明兩個處理事件的對象
/** cell 點擊事件 */
@property(nonatomic,strong)RACCommand *itemClickCommand;
/** 刷新數(shù)據(jù) */
@property(nonatomic,strong)RACCommand *refreshCommand;
//RACCommand RAC中用于處理事件的類,可以把事件如何處理,事件中的數(shù)據(jù)如何傳遞酌壕,包裝到這個類中掏愁,他可以很方便的監(jiān)控事件的執(zhí)行過程。
聲明 數(shù)據(jù)源和分頁
@property(nonatomic,strong)NSMutableArray *data;
@property(nonatomic,assign)NSInteger page;
初始化事件 并綁定數(shù)據(jù)
-(void)initViewModel{
@weakify(self);
self.refreshCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
@strongify(self)
//此處 網(wǎng)絡(luò)請求獲取數(shù)據(jù)源 上下拉刷新更新數(shù)據(jù)源
self.page = [input[1] integerValue];
if (self.page == 1) {
[self.data removeAllObjects];
}
for (NSInteger i = 0; i< 5; i++) {
NSDictionary *dic = @{@"name":[NSString stringWithFormat:@"第%ld頁怪獸",self.page],@"age":@(i+20)};
Model *mod = [[Model alloc] initWithDictionary:dic];
[self.data addObject:mod];
}
TableView *tableView = input[0];
[tableView reloadData];
[tableView.mj_header endRefreshing];
[tableView.mj_footer endRefreshing];
return [RACSignal empty];
}];
self.itemClickCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
@strongify(self)
HeadView * view = input[1];
NSInteger index = [input[0] integerValue];
Model *mod = self.data[index];
view.bgLabel.text = [NSString stringWithFormat:@"--%@: %ld歲--",mod.name,mod.age];
return [RACSignal empty];
}];
![MVVM.gif](http://upload-images.jianshu.io/upload_images/1711499-b8eeebb6add0a61d.gif)
}
最后就是在VC中 綁定VM
//最后的VC 僅僅只有50行代碼 所有的業(yè)務(wù)邏輯卵牍,網(wǎng)絡(luò)請求都已經(jīng)放在了VM中進行果港。
@interface TestViewController ()
@property(nonatomic,strong) TableView *tableView;
@property(nonatomic,strong) ViewModel *viewModel;
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.viewModel = [ViewModel new];
[self bindViewModel];
[self.view addSubview:self.tableView];
}
-(void)bindViewModel{
//KVO 形式 動態(tài)監(jiān)測 數(shù)組和 頁數(shù)
RAC(self.tableView,dataArray) = RACObserve(self.viewModel, self.data);
RAC(self.tableView,page) = RACObserve(self.viewModel, self.page);
[self.viewModel.refreshCommand execute:@[self.tableView,@(1)]];
}
-(TableView*)tableView{
if (!_tableView) {
_tableView = [[TableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_tableView.viewModel = self.viewModel;
}
return _tableView;
}
隨便看了一點別人的demo和資料,深點的還不太懂糊昙, 很多類還沒有嘗過辛掠,需要多多嘗試。
參考鏈接: