首页 > 解决方案 > 使用 .grouped tableview 时,摆脱在我的标题中添加的空间

问题描述

图像显示当 tableview 设置为 .grouped 时添加的白色边框。我制作了 tableview .grouped ,这样标题就不会粘了。但是,这会导致在 tableview 的顶部和底部添加额外的填充(我已修复),以及在我的标题视图顶部添加额外的空间。在网上,似乎人们唯一在谈论的是摆脱标题,但标题是我将它分组的原因,因为这样可以防止它粘住。如何摆脱 .grouped 添加到所有标题的 20 左右像素的填充而不摆脱我的标题视图?撤消 .grouped 设置会消除空白。这是我想保持分组的唯一方法吗?

你会认为苹果会让这件事变得更容易。

我将标题涂成红色以突出上面不需要的空白。更改单元格所在行的高度不会影响白色区域。

标签: swiftuitableview

解决方案


这将摆脱整个 tableView 的 headerView 和 footerView:

    tableView.tableHeaderView = UIView(frame: CGRect(x: CGFloat.leastNonzeroMagnitude, y: CGFloat.leastNonzeroMagnitude, width: tableView.bounds.size.width, height: CGFloat.leastNonzeroMagnitude))
    tableView.tableFooterView = UIView(frame: CGRect(x: CGFloat.leastNonzeroMagnitude, y: CGFloat.leastNonzeroMagnitude, width: tableView.bounds.size.width, height: CGFloat.leastNonzeroMagnitude))

然后要单独将节标题的高度设置为 0,您必须使用以下函数:

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

对于每个部分的页脚,类似地:

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

推荐阅读