EPSReactiveList 提供了 UITableViewController
和 UICollectionViewController
的子类,它们自动填充一个表格/收集视图,并通过观察模型对象的数组的变化来自动处理行的插入和删除。它使用 ReactiveCocoa,并设计用于与 MVVM 模式一起使用。
EPSReactiveList 替换了 EPSReactiveTableViewController 和 EPSReactiveCollectionViewController。
子类化 EPSReactiveTableViewController
或 EPSReactiveCollectionViewController
,并编写一个调用 setBindingToKeyPath:onObject:
的 init
方法来设置绑定。关键路径的值必须始终是一个包含实现了 -isEqual:
和 -hash
方法的对象的 NSArray
。数组中不应出现重复的对象。在 init
方法中,为将包含在观察到的数组中的对象的类注册一个单元格类。(单元格类必须遵守 <EPSReactiveListCell>
。)
- (id)init {
self = [super initWithStyle:UITableViewStylePlain];
if (self == nil) return nil;
_viewModel = [EPSExampleViewModel new];
[self setBindingToKeyPath:@"sortedNotes" onObject:_viewModel];
[self registerCellClass:[EPSNoteCell class] forObjectsWithClass:[EPSNote class]];
...
return self;
}
要响应用户对单元格的点击,重写 -tableView:didSelectRowForObject:atIndexPath:
或 -collectionView:didSelectItemForObject:atIndexPath:
中的一个,具体取决于你是否子类化了 EPSReactiveTableViewController
或 EPSReactiveCollectionViewController
。
- (void)tableView:(UITableView *)tableView didSelectRowForObject:(id)object atIndexPath:(NSIndexPath *)indexPath {
// Do something with `object`
}
您不需要编写任何数据源方法。
有关如何使用 EPSReactiveTableViewController
和 EPSReactiveCollectionViewController
的更多示例,请参阅 示例项目。
要运行示例项目;克隆仓库,并首先从项目目录运行 pod install
。
EPSReactiveTableViewController 需要 ReactiveCocoa 2.3 或更高版本。
EPSReactiveList 通过 CocoaPods 提供——要安装它,只需将以下行添加到您的 Podfile 中:
pod "EPSReactiveList"
或者,将 Classes 目录中的文件包含到您的项目中,并按照它们的 安装说明 安装 ReactiveCocoa 2.3。
EPSReactiveList遵循MIT许可证。有关更多信息,请参阅LICENSE文件。