如何在 UITableView 部分之间添加 space?

How to add space between UITableView sections?

iOS 编辑联系人 UITableView 如何在部分之间添加 space 同时仍显示前面部分的最后一个单元格的分隔符?

在下面的屏幕截图中,第三部分的第一个单元格突出显示。如何在其上方添加白色 space 而不隐藏其上方 "add phone" 单元格下方的分隔符?我尝试添加一个白色部分 header,但这会隐藏其上方单元格的分隔符。

作为技巧,我刚刚在 header 视图中添加了一个分隔符。

// MARK: UITableViewDelegate

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return section == 0 ? UITableViewAutomaticDimension : 24
}

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 0 {
        return nil
    } else {
        let view = UIView(frame: CGRectZero)
        view.backgroundColor = UIColor.whiteColor()
        let separator = UIView(frame: CGRect(x: tableView.separatorInset.left, y: 0, width: 0, height: 0.5))
        separator.autoresizingMask = .FlexibleWidth
        separator.backgroundColor = tableView.separatorColor
        view.addSubview(separator)
        return view
    }
}

如果您有干净简单的解决方案,请回答。

如果您只关心间距,请尝试以下代码:

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

在 iOS 15 上,您可以向 header

部分添加填充
if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = 0
}