swift - 使用 .grouped tableview 时消除标题中添加的空间

标签 swift uitableview

Image shows white border added when tableview is set to .grouped.我将 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/

相关文章:

ios - CAGradientLayer 位置不正确

ios - 我正在尝试将命令从 SKScene(SpriteKit) 传递到 SCNScene(SceneKit)

objective-c - 在 Swift 中展开 Optional 值错误

ios - 来自相机/相机胶卷的图像仅上传到 Firebase 一次

ios - 带有 iAd 横幅的 UITableView 出现奇怪的溢出

ios - 需要帮助以更好地了解Objective C中的MVC

ios - 如何缩放应用程序,使 iPhone 6 与 iPhone 5 SpriteKit 相同(缩放)

ios - UITableView 和 UICollectionView 的通用单元格

iphone - 如何在 UITableView 上延迟加载文本?

iphone - UITableView 缓慢滚动