为UITableView节头返回CGFloat.leastNormalMagnitude会导致崩溃

问题描述 投票:8回答:4

我为iOS 8制作了一个应用程序,它使用分组的UITableView作为其页面之一。其中有多个部分使用CGFloat.leastNormalMagnitude(或Swift 2及更低版本中的CGFloat.min)用于剖面页眉和页脚高度以删除“默认”空间。一切顺利,直到应用程序在iOS 9和10中运行,它崩溃时出现此错误:

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'section header height不能为负 - 提供的第0部分的高度为-0.00000'

不知何故,1下的任何值(除了舍入的0)都被视为否定 - 并且使用1作为返回值将使页眉/页脚空间再次出现。

有没有解决方法来解决这个问题?

提前致谢。

ios swift uitableview ios9 ios10
4个回答
18
投票

我为tableView(_:heightForHeaderInSection:)尝试了几个值,并发现:

  • leastNormalMagnitudeleastNonzeroMagnitude将被视为减号(因此崩溃)。
  • 零将使TableView返回默认高度作为页眉/页脚。
  • 零和一之间的任何东西都将被视为减号。
  • 一个将使TableView返回默认高度。
  • 多于一个(例如1.1)的任何内容都会将页眉/页脚设置为实际高度。

我最终使用1.1来解决我的问题。

希望这能帮助那里的人!


3
投票

我们使用运行Xcode 10.2的Swift 5在iOS 9上经历了相同的运行。事实证明,如果您在estimatedHeightForHeaderInSection / estimatedHeightForFooterInSection中返回CGFloat.leastNormalMagnitude或CGFloat.leastNonzeroMagnitude,它将在iOS 9设备上崩溃。

您仍然可以在heightForHeaderInSection / heightForFooterInSection中返回leastNormalMagnitude或leastNonzeroMagnitude。

正如edopelawi在上面指出的那样,任何小于1的值都将被视为负数,1将被视为默认的分组部分页眉/页脚高度。如果设备运行iOS 10或更低版本,我们最终返回1.000001作为估计高度:


func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
    if #available(iOS 11.0, *) {
        return self.tableView(tableView, heightForHeaderInSection: section)
    } else {
        return max(1.000001, self.tableView(tableView, heightForHeaderInSection: section))
    }
}

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

0
投票

如果我设置节标题高度,我有同样的问题:

tableView.sectionHeaderHeight = UITableViewAutomaticDimension
tableView.estimatedSectionHeaderHeight = CGFloat.leastNormalMagnitude

但是如果我将我的Controller设置为我的表视图的委托(UITableViewDelegate)并实现:

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

然后它工作


0
投票

也许CGFloat.leastNonzeroMagnitude是你需要的!

© www.soinside.com 2019 - 2024. All rights reserved.