阻止 TableView backgroundView 越过每个部分的最后一个单元格?

Stop TableView backgroundView from going past last cell in each section?

我在 UIViewController 中有一个 UITableView 嵌套在 UITabBarController 中,每个部分都有一个自定义部分 header,它是 UIView 的子类. UItableView 在代码和故事板中设置了所有适当的委托和数据源。

页脚在代码中明确设置为 0。

无论出于何种原因,背景(下面的红色部分)似乎都从每个部分的每个 UITableViewCell 渗出。

我的 UITableView 目前看起来像这样:

我在情节提要中对表视图的设置如下所示:

最后,这里是控制 tableView 的代码,作为 UITableView 子类的扩展编写:

extension TestViewController: UITableViewDelegate {
 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 64
}

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

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

extension TestViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {


    let view = TestHeaderView()
    view.setLabelWithValues(valueType: "Example", amount: 1)
    return view
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath) as! TestTableViewCell
    cell.testLabel?.text = "example"
    cell.testLabel2?.text = "example"
    cell.testLabel3?.text = "example"
    return cell
}

}

如何防止背景穿过每个单元格部分?

想通了:

看起来我必须在故事板大小检查器列中将页脚大小设置为 0(然后默认为 1),如下所示:

这将留下单个像素宽度的页脚,因此请将背景颜色切换为您的单元格的颜色。