我将 tableview .grouped 进行了处理,这样标题就不会粘在一起。但是,这会导致在表格 View 的顶部和底部添加额外的填充(我已修复),以及在标题 View 顶部添加额外的空间。在网上,人们谈论的唯一一件事似乎是摆脱标题,但标题就是我将其分组的原因,因为这样可以阻止它粘在一起。如何消除 .grouped 添加到所有标题的 20 个左右像素的填充,而不消除我的标题 View ?撤消 .grouped 设置会消除空白。这是我想要将其分组的唯一方法吗?
你可能会认为 Apple 会让这一切变得更容易。
我将标题染成红色以突出显示上面不需要的空白。更改单元格行的高度不会影响白色区域。
最佳答案
这将消除整个 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
}
关于swift - 使用 .grouped tableview 时消除标题中添加的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62221090/