UITableview中各节之间的空间

Joe*_*oel 46 layout uitableview ios swift

我需要减少两个部分之间的空间UITableView.我看了这个问题,但解决方案不允许我的自定义标题视图,因为它结合了页脚和标题的空间.

这是一张照片UITableView.黑色是UITableView背景颜色.

tableview截图

roc*_*ift 243

在 iOS 15 上,您可能想要减少sectionHeaderTopPadding

if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = 0
}
Run Code Online (Sandbox Code Playgroud)

  • 多谢。苹果默认引入非零填充的方式太糟糕了。史诗 (6认同)

Ica*_*aro 107

您是否尝试覆盖此功能:

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNormalMagnitude
}
Run Code Online (Sandbox Code Playgroud)

  • 对于Swift 3,您可以返回CGFloat.leastNormalMagnitude.谢谢Icaro! (17认同)
  • 仅当您还重写 viewForFooterInSection 方法时,这才有效。你可能会在这个方法中返回nil(至少在iOS14中,我在以前的版本中没有尝试过)。 (5认同)
  • 是的,它不起作用,我认为该函数的最小值是 1 (2认同)
  • 我注意到你不能在 heightForFooterInSection 中返回 0,尝试返回 0.00001f 作为高度,你会得到 0 像素/点的高度。 (2认同)

Sud*_*vis 39

我认为您可以通过在页脚板或XIB中将页脚视图高度调整为min来解决此问题.在此输入图像描述

我不知道你在代码中写的页脚高度.对不起,如果我错了.

UITableView隐藏页脚视图的可能重复项


Ali*_*ris 13

对于Swift 3

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}
Run Code Online (Sandbox Code Playgroud)

  • @DrewC您确实在表视图控制器中覆盖它。 (5认同)
  • 那是一个 UITableViewDelegate 方法。你不要覆盖它。 (2认同)

Lad*_*d.c 13

对于 Swift 4+ 你需要实现这两个方法

extension MyViewController : UITableViewDelegate {

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 好的!不知道为什么这没有更多的赞成票。在“viewForFooterInSection”中返回“UIView”或“nil”非常重要。仅使用“heightForFooterInSection”(如其他答案中提到的那样不起作用) (2认同)

Fer*_*nas 10

对于 Swift 5+:

默认情况下,页眉和页脚有一些空间。这就是为什么我遇到了为各部分设置精确分隔的问题。

我对两个部分之间的分离的解决方案如下:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 0 {
        return 24
    } else if section == 1 {
        return 32
    } else {
        return 40
    }
}
    
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    nil
}
    
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    nil
}
    
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    CGFloat.leastNormalMagnitude
}
Run Code Online (Sandbox Code Playgroud)

正如您在 viewForFooterInSection 和 viewForHeaderInSection 中看到的那样,我需要返回 nil。

如果您只想更改 footerHeight,只需为 heightForHeaderInSection 返回 CGFloat.leastNormalMagnitude,并返回 heightForFooterInSection 中每个部分的高度。


Aks*_*Aks 6

您需要使用方法heightForHeaderInSection来定义标题和单元格文本之间的空间.您也可以根据不同的部分进行更改,例如.在某些部分你可能需要显示更多的距离和一些,你不想显示差距.对于这种情况,您可以使用0.000001f的CGFLOAT_MIN.举个例子,如何使用不同的标题高度的不同部分

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 2)
    {
        return 55.0;
    }
    else
    {
        return CGFLOAT_MIN;
    }
}
Run Code Online (Sandbox Code Playgroud)

希望它会对你有所帮助.

  • 这是错误的。heightForHeaderInSection 定义该部分的 headerView 的高度,而不是它们之间的空间,这就是OP所询问的。https://developer.apple.com/reference/uikit/uitableviewdelegate/1614855-tableview?language=objc (2认同)

小智 6

我只需减少表格视图部分标题的顶部填充:

tableView.sectionHeaderTopPadding = 0
Run Code Online (Sandbox Code Playgroud)


Nic*_*lio 5

除了Icaro发布答案外,我还想补充一点,您还需要实现要tableView:viewForFooterInSection:返回nil的部分的方法,以删除下面的空白部分,该方法将变为:

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
Run Code Online (Sandbox Code Playgroud)