ios - UITableView contentSize 太大了 10pt(仅限 iOS 10!)
问题描述
我有一个奇怪的问题。我有一个UITableView
这样的集合:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200.0
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 210.0
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if drawerState == .closed {
return 0
}
return 1
}
最初drawerState
是.closed
这样contentSize
的210
。然后我点击一个按钮来改变状态并打印新的内容大小,如下所示:
drawerState = .assessmentOpen
print("old content size is \(tableView.contentSize.height)")
tableView.insertRows(at: [IndexPath(item: 0, section: 0)], with: .none)
print("new content size is \(self.tableView.contentSize.height)")
我得到的结果是:
old content size is 210.0
new content size is 420.0
新的内容大小比它应该的大 10pt(新的内容大小应该是 410)。这个多余的高度是从哪里来的?奇怪的是,如果我在延迟后再次打印该值是正确的。我已经尝试了所有组合,layoutIfNeeded
并且使用DispatchQueue.main.async
该值始终比应有的值高 10pt。我究竟做错了什么?注意:它只在 iOS10 上运行,iOS11 没有问题。
解决方案
如果你有一个.grouped
-styled UITableView
,那么每个部分的页脚都有一个不为零的默认高度。所以要解决这个问题,请添加:
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
如果你只返回0
. 它回退到默认值。从技术上讲,CGFloat.leastNormalMagnitude
它非常类似于0
. 但如果你比较它 a0
小于CGFloat.leastNormalMagnitude
。
推荐阅读
- c# - 如何向我的用户角色表添加第三个属性?
- python - 在非数字上拆分字符串
- powershell - powershell 比较文件日期无法始终如一地工作
- python - 使用列表推导查找列表中的元素,其中另一个列表中的所有元素都是因子
- c# - MetadataType 和 Attribute.IsDefined
- regex - 如何在第一次搜索时结束正则表达式
- haskell - 涉及预期类型的错误
- python - 如何遍历列表网络抓取的表格列并为每个项目返回一个结果?
- javascript - 将组元素重新排序为特定顺序
- javascript - React Native - 应用程序在运行和应用“setInterval”后崩溃