Ios 显示空节的表视图分隔符

Ios 显示空节的表视图分隔符,ios,uitableview,Ios,Uitableview,我将UITableView设置为UITableView样式分组样式。每当节有一个或多个项目时,它都会在节标题下显示一个单像素分隔符。但是,如果节没有项目,则节之间没有分隔符 如何使分隔符出现在所有部分中,即使是没有单元格的部分 请注意,第1节和第2节与第一个单元格之间有分隔符,而第3节与第4节之间没有分隔符。您可以尝试自定义节标题视图。使用此委托函数:-(UIView*)tableView:(UITableView*)tableView for HeaderInSection:(NSInteg

我将
UITableView
设置为
UITableView样式分组
样式。每当节有一个或多个项目时,它都会在节标题下显示一个单像素分隔符。但是,如果节没有项目,则节之间没有分隔符

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


请注意,第1节和第2节与第一个单元格之间有分隔符,而第3节与第4节之间没有分隔符。

您可以尝试自定义节标题视图。使用此委托函数:-(UIView*)tableView:(UITableView*)tableView for HeaderInSection:(NSInteger)节

使用这两种方法在节之间添加分隔符

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];

    [headerView setBackgroundColor:[UIColor brownColor]];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 43, 320, 1)];
                    img.backgroundColor = [UIColor whiteColor];

    [headerView addSubview:img];
    return headerView;
}

一种解决方案是为每个空节添加页脚:

- (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;
    }
}
注:

  • 这将在表视图中显示
  • 它不会在heightForFooterInSection中返回0,而是返回
这通常会产生正确的结果:

。。。但有几个问题:

  • 这会利用页脚,因此如果需要显示页脚,此解决方案可能无法工作
  • 在编辑模式下,当您将最后一个项目从节中拖出时,该节与其下一节之间的边框将消失
  • 在编辑模式下,将项目拖动到空部分时,该部分底部将有一个双边框
  • 问题2和3的示例:

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

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