【问题标题】:Returning CGFloat.leastNormalMagnitude for UITableView section header causes crash为 UITableView 部分标题返回 CGFloat.leastNormalMagnitude 会导致崩溃
【发布时间】:2017-07-03 22:09:48
【问题描述】:

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

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“节标题高度不得为负 - 提供第 0 节的高度为 -0.00000”

不知何故,1 下的任何值(四舍五入的0 除外)都被视为负数 - 使用1 作为返回值会使页眉/页脚空间再次出现。

有什么解决方法可以解决这个问题吗?

提前致谢。

【问题讨论】:

  • 那么 CGFloat.leastNormalMagnitude 是否被视为负数,或者任何低于 1.0 的值都被视为负数?
  • 是的,我不知道为什么:|
  • 你试过只返回0吗?这对我有用
  • 返回0 会使页眉/页脚上的空间...

标签: ios swift uitableview ios9 ios10


【解决方案1】:

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

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

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

希望这会对那里的人有所帮助!

【讨论】:

  • 我不明白您是如何通过设置 1.1 解决问题的。如果将高度设置为 1.1,您的节标题仍然会获得非零空间吗?
  • @Salah 是的,我最终用与表格视图使用相同背景颜色的视图填充了 1.1 空间。
  • 这只是可悲的行为。
  • @JakubTruhlář 和无证,最重要的是......但是,生活还在继续:)
【解决方案2】:

我们在 iOS 9 上使用运行 Xcode 10.2 的 Swift 5 体验了相同的运行。事实证明,如果你在estimateHeightForHeaderInSection/estimateHeightForFooterInSection 中返回CGFloat.leastNormalMagnitude 或CGFloat.leastNonzeroMagnitude,它会在iOS 9 设备上崩溃。

您仍然可以在 heightForHeaderInSection / heightForFooterInSection 中返回 minimumNormalMagnitude 或 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
}

【讨论】:

    【解决方案3】:

    如果我用以下方式设置节标题高度,我也会遇到同样的问题:

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

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

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

    然后就可以了

    【讨论】:

    • 感谢您的回复,但我的问题出在UITableviewDelegate 设置上,而没有手动设置tableView 的sectionHeaderHeightestimatedSectionHeaderHeight :(
    【解决方案4】:

    也许CGFloat.leastNonzeroMagnitude 是你需要的!

    【讨论】:

    • 感谢您的回答,但它仍然与该值崩溃:(
    【解决方案5】:

    如果你实现了 viewForHeader 和 viewForFooter,你就不必作弊了。

    例如,当我想隐藏页眉和/或页脚时,我个人会返回 0.1f。 此示例将完全隐藏页眉和页脚,没有空间可填充,您可以从中添加自定义逻辑。

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return 0.1f;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
        return 0.1f;
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
         return nil;
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        return nil;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多