测试已测试 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2016年4月 |
由 Gwenn Guihal 和 Aymen Farrah 维护。
TableViewDescriptor 是一个库,用于以数据导向的方式而不是索引导向的方式结构化您的 UITableview 实现。
与其使用索引导向的方法来描述您的 TableView,不如使用 TableViewDescriptor 提出的数据导向方式。TableViewDescriptor 使用代码块而不是从 UITableViewDataSource
和 UITableViewDelegate
中实现方法(例如 heightForRowAtIndexPath:
或 cellForRowAtIndexPath:
)
实例化一个 TableViewDescriptor 然后将它设置为 tableView 的代理和数据源
self.tableViewDescriptor = [[VSTableViewDescriptor alloc] init];
self.tableView.delegate = self.tableViewDescriptor;
self.tableView.dataSource = self.tableViewDescriptor;
添加标题分区非常简单
VSSectionDescriptor* sectionDescriptor = [[VSSectionDescriptor alloc] initHeaderSectionWithTitle:^NSString *(UITableView* tableView, int section)
{
return @"Section Title";
}];
[self.tableViewDescriptor addSectionDescriptor:sectionDescriptor];
空分区
VSSectionDescriptor* sectionDescriptor = [[VSSectionDescriptor alloc] initEmpty];
[self.tableViewDescriptor addSectionDescriptor:sectionDescriptor];
浏览模型并在 tableView 中添加单元格
__weak typeof(self) weakSelf = self; // important, use weak self in block
for (ModelElement* element in self.myModel)
{
VSCellDescriptor* cellDescriptor = [[VSCellDescriptor alloc] initWithHeight:^CGFloat(UITableView* tableView, NSIndexPath *indexPath)
{
return [VSCellViewTableViewCell height:element];
} configure:^UITableViewCell *(UITableView* tableView, NSIndexPath *indexPath)
{
VSCellViewTableViewCell* cell = (VSCellViewTableViewCell*)[weakSelf.tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
[cell configure:element];
return cell;
} select:^(UITableView* tableView, NSIndexPath *indexPath)
{
[weakSelf onTap:element];
}];
[sectionDescriptor addCellDescriptor:cellDescriptor];
}
就是这样!
TableViewDescriptor 不实现 UITableView 的所有代理和数据源方法。您可以使用您的控制器作为代理同时使用 TableViewDescriptor 对于缺失的方法,或者为项目做出贡献 :)
self.tableViewDescriptor.dataSource = self;
self.tableViewDescriptor.delegate = self;
TableViewDescriptor 实现的方法
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
如果您在控制器中重写通过 TableViewDescriptor 实现的方法,会在控制台发出警告。
VSSectionDescriptor* sectionDescriptor = [[VSSectionDescriptor alloc] initEmpty];
//...
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; // console output: WARNING : YourController overrides VSTableViewDescriptor::numberOfSectionsInTableView:
}
您可以下载项目查看集成的示例。
TableViewDescriptor由Voyages-sncf.com拥有并维护。
TableViewDescriptor最初由Gwenn Guihal创建。
TableViewDescriptor采用MIT许可。