测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布日期最后发布 | 2016年10月 |
由 Hai Feng Kao 维护。
依赖 | |
KVOMutableArray | >= 0 |
WZProtocolInterceptor | >= 0 |
UITableView 和 UICollectionView 绑定
此项目提供了一些辅助函数,将可变数组绑定到表格视图或集合视图。它支持单节或多节。
HFViewBinding 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中:
pod "HFViewBinding"
KVOMutableArray* someData = [[KVOMutableArray alloc]
initWithMutableArray:[@[@"cell 1", @"cell 2"] mutableCopy]];
HFTableViewBinding* bindingHelper =
[HFTableViewBinding bindingForTableView:self.tableView
sourceList:someData
didSelectionBlock:^(id model)
{ NSLog(@"clicked on %@", model); }
templateCellClassName:@"YourCellClass"
isNested:NO];
bindingHelper.delegate = self;
bindingHelper.dataSource = self;
您必须使用 KVOMutableArray
作为可变数组类来通知观察者关于突变事件。有关更多详情,请参阅 KVOMutableArray。
单元格必须实现 HFBindingDelegate
协议。
delegate
和 dataSource
设置是可选的。如果已设置,则所有未被绑定助手处理的委托方法都将发送到上面的示例中的 self
。
为了支持多节,应将 isNested
设置为 YES
。数组中的每个项都必须是 KVOMutableArray
的类。
KVOMutableArray* firstRow = [[KVOMutableArray alloc]
initWithMutableArray:[@[@"cell 1", @"cell 2"] mutableCopy]];
KVOMutableArray* secondRow = [[KVOMutableArray alloc]
initWithMutableArray:[@[@"cell 1", @"cell 2"] mutableCopy]];
KVOMutableArray* someData = [[KVOMutableArray alloc] initWithMutableArray:[@[firstRow, secondRow] mutableCopy]];
HFTableViewBinding* bindingHelper =
[HFTableViewBinding bindingForTableView:self.tableView
sourceList:someData
didSelectionBlock:^(id model)
{ NSLog(@"clicked on %@", model); }
templateCellClassName:@"YourCellClass"
isNested:YES];
MVVM (模型-视图-视图模型) 是一种流行的 MVC 架构替代方案。它将 UI 逻辑从难以测试的控制器移动到可测试的视图模型中。MVVM 重度依赖于视图绑定。但现有的绑定库(HRTableCollectionBindingHelper,CETableViewBinding)不支持多节表格视图。此外,它们实现中的表格视图插入和删除动画被禁用,这真的很让我烦恼 :(
API 来自于 HRTableCollectionBindingHelper。
委托转发想法来自 CETableViewBinding。
向 Rannie 和 Colin 表示衷心的感谢。
需要 iOS 7.0 和 ARC。
Hai Feng Kao,[email protected]
欢迎补丁、拉取请求和其他反馈。
HFViewBinding遵循MIT许可证。有关更多信息,请参阅LICENSE文件。