我将 UITableView
设置为 UITableViewStyleGrouped
样式。只要一个部分有一个或多个项目,它就会在部分标题下方显示一个像素分隔符。但是,如果该部分没有项目,则各部分之间没有分隔符。
如何让分隔符出现在所有部分,即使是那些没有单元格的部分?
请注意第 1 部分和第 2 部分与其第一个单元格之间有分隔符,但第 3 部分与第 4 部分之间没有分隔符。
最佳答案
一种解决方案是为每个空白部分添加一个页脚:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return [self tableView:tableView numberOfRowsInSection:section] == 0 ? 0.5 : 0.00000001;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if ([self tableView:tableView numberOfRowsInSection:section] == 0) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.5)];
view.backgroundColor = tableView.separatorColor;
return view;
} else {
return nil;
}
}
注意事项:
- 这uses the separatorColor to ensure it is consistent with the other lines在 TableView 中。
- 不是在 heightForFooterInSection 中返回 0,而是 returns a very small number instead .
大多数情况下这会产生正确的结果:
...但是有几个问题:
- 这会利用页脚,因此如果您需要显示页脚,此解决方案可能行不通。
- 在编辑模式下,当您将最后一个项目拖出一个部分时,该部分与其下方的部分之间的边框将消失。
- 在编辑模式下,当您将项目拖入空白部分时,该部分底部会出现双边框。
问题 2 和 3 的示例:
在上图中,请注意第 2 部分和第 3 部分之间没有线,因为所有项目都已移出该部分(问题 2)。
还要注意第 3 节和第 4 节中的最后一项如何具有双边框,因为它们被拖到新的节中(问题 3)。
关于ios - 显示空白部分的 TableView 分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24666122/