YXWListBinder 1.0.1

YXWListBinder 1.0.1

xiaowinner 维护。



  • 原晓文

YXWListBinder

使用

只需要进行以下步骤

  • Podfile 中输入 pod 'YXWListBinder'
  • 将 YXWListBinder 文件夹下的所有文件移动到您的项目中.
  • 运行 pod install.

注意

0.2.0 依赖的是老版本 ReactiveCocoa >= 2.5,0.3.0 之后依赖最新的 ReactiveObjC

提示 :)

  • 该项目依赖于 ReactCocoa,请在 Podfile 中添加 pod 'ReactiveObjC'.

  • 项目中包含 Demo.

  • 根据您的需求创建自己的 YXWListBinderWidgetProtocol 和 YXWListBinderViewModelProtocol

  • Protocol 中需要有 MVVM 的绑定机制的方法,例如:

/*
Cell
*/
- (void)bindViewModel:(id<YXWListBinderViewModelProtocol>)viewModel atIndexPath:(NSIndexPath *)indexPath;
/*
ViewModel
*/
@required
- (NSString *)identifier;
- (CGFloat)widgetHeight;

@optional
- (NSInteger)gainSubDataCount:(NSInteger)section;
- (id <YXWListBinderViewModelProtocol>)gainSubData:(NSInteger)index;
  • 注意区分以下两个初始化方法:
/*
根据 nib 注册TableView Cell
*/
- (instancetype)initBinder:(UITableView *)tableView
dataCommand:(RACCommand *)dataCommand
hasSection:(BOOL)hasSection
nibsCell:(NSArray *)nibs
identifiers:(NSArray *)identifiers;
/*
根据 Class name 注册TableView Cell
*/
- (instancetype)initBinder:(UITableView *)tableView
dataCommand:(RACCommand *)dataCommand
hasSection:(BOOL)hasSection
cellClassNames:(NSArray *)names
identifiers:(NSArray *)identifiers;
/*
根据 nib 注册CollectionView Item
*/
- (instancetype)initBinder:(UICollectionView *)collectionView
nibsItem:(NSArray *)nibsItem
itemIdentifiers:(NSArray *)itemIdentifiers
dataCommand:(RACCommand *)dataCommand;
/*
根据 Class name 注册CollectionView Item
*/
- (instancetype)initBinder:(UICollectionView *)collectionView
itemClassNames:(NSArray *)itemClassNames
itemIdentifiers:(NSArray *)itemIdentifiers
dataCommand:(RACCommand *)dataCommand;

概念

我们大多数构建页面的基本都可以使用 TableView 或者 CollectionView 实现,binder 的意义在于使用 MVVM 模式开发,更注重在 ViewModel 的数据流控制来拼装页面的子视图,把 ViewController 只作为一个通道。

一个简单的 Collection 页面搭建:1. 创建控制器,继承自 YXWBaseCollectionViewController。2. 初始化控制器对应的 ViewModel。3. 注册页面要展示的 cell,例如:

NSArray *cellNibs = @[
[UICollectionViewCell nibFromModule]
];

NSArray *cellIdentifiers = @[
[UICollectionViewCell className]
];

NSArray *headerNibs = @[
[UICollectionViewHeader nibFromModule]
];

NSArray *headerIdentifiers = @[
[UICollectionViewHeader className]
];

self.listBinder = [[YXWListBinder alloc] initBinder:self.collectionView
nibsItem:cellNibs
nibHeaders:headerNibs
itemIdentifiers:cellIdentifiers
headerIdentifiers:headerIdentifiers
dataCommand:self.viewModel.dataCommand];

4. 重写 requestAndCombinedData,给 ViewModel 的默认请求 command 传参数。

- (void)requestAndCombinedData {
   RACTuple *tuple = [RACTuple tupleWithObjects:self.category,more,nil];
   [self.viewModel.dataCommand execute:tuple];
}

5. 在 ViewModel 中重写 requestData 方法组装展示的视图。Model 的概念就是展示的控件,所以每个 Model 应该遵守 YXWListBinderViewModelProtocol 协议,实现对应控件的 identifier 方法,widgetHeight 方法。这样在 ViewModel 中请求到的 Model 数据添加到数组时,就可以当作一个个需要展示的视图添加到数组中。