BRTableView
目的
解耦ViewController和UITableViewCell,在cell类型很多时,更能显示出便捷。完全不需要在代理方法中判断各种类型,返回相应的cell和高度。cell的增加和删除也更好控制,只需改动一个地方,减小出错概率。在个人中心、设置、电商app首页等使用中,维护会非常方便。
使用
- 声明一个BRTableView
- (BRTableView *)tableView {
if (!_tableView) {
_tableView = [[BRTableView alloc] init];
_tableView.tableFooterView = [UIView new];
}
return _tableView;
}
- 将其添加到视图,并指定sections
[self.view addSubview:self.tableView];
self.tableView.frame = self.view.bounds;
// 执行这句会自动reloadData
self.tableView.sections = self.viewModel.sections;
- section初始化如下
DemoCellViewModel *cell1 = [DemoCellViewModel new];
cell1.title = @"亚索";
DemoCellViewModel *cell2 = [DemoCellViewModel new];
cell2.title = @"小法师";
DemoCellViewModel *cell3 = [DemoCellViewModel new];
cell3.title = @"Flutter";
BRTableViewSection *section1 = [[BRTableViewSection alloc] initWithSectionKey:@"" viewModels:@[cell1, cell2, cell3]];
self.sections = @[section1];
在上面的例子中只有一个cell,DemoCell,它有一个对应的viewModel,DemoCellViewModel。在编写时,cell不会出现在vc中。只需组装好cell的viewModel,添加到section中。
需求
iOS8+
安装
BRTableView可以通过CocoaPods获取。要安装它,只需将以下行添加到Podfile中
pod 'BRTableView'
作者
[email protected],[email protected]
许可证
BRTableView在MIT许可证下可用。更多信息请参阅LICENSE文件。