ios - 显示空白部分的 TableView 分隔符

标签 ios uitableview

我将 UITableView 设置为 UITableViewStyleGrouped 样式。只要一个部分有一个或多个项目,它就会在部分标题下方显示一个像素分隔符。但是,如果该部分没有项目,则各部分之间没有分隔符。

如何让分隔符出现在所有部分,即使是那些没有单元格的部分?

enter image description here

请注意第 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;
    }
}

注意事项:

大多数情况下这会产生正确的结果:

enter image description here

...但是有几个问题:

  1. 这会利用页脚,因此如果您需要显示页脚,此解决方案可能行不通。
  2. 在编辑模式下,当您将最后一个项目拖出一个部分时,该部分与其下方的部分之间的边框将消失。
  3. 在编辑模式下,当您将项目拖入空白部分时,该部分底部会出现双边框。

问题 2 和 3 的示例:

enter image description here

在上图中,请注意第 2 部分和第 3 部分之间没有线,因为所有项目都已移出该部分(问题 2)。

还要注意第 3 节和第 4 节中的最后一项如何具有双边框,因为它们被拖到新的节中(问题 3)。

关于ios - 显示空白部分的 TableView 分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24666122/

相关文章:

ios - Swift 4 CATransition 翻转 UIView 无 3D 效果

ios - 错误 : expected a property attribute before 'atomic'

ios cocoa : How to adjust the size of the popover depending on the number of rows in uitableview

ios - 属性和 ivars 的问题

swift - 单击 UITableView 时没有任何反应(尝试在 View 之间发送数据)

ios - UITableViewCell 点击悬停

ios - SwiftUI 添加自定义 UIViewControllerTransitioningDelegate

ios - ShareKit - Flickr 集成 - 想要绕过(或至少自动填充)属性对话框

ios - 与名为 com.apple.commcenter.coretelephony.xpc 的服务的连接已失效

ios - Swift 核心数据到自定义 UITableViewCell UILabel 错误