gpt4 book ai didi

ios - UITableView 在我实现 `estimatedHeightForRowAt indexPath` 后崩溃?

转载 作者:可可西里 更新时间:2023-11-01 00:39:16 24 4
gpt4 key购买 nike

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
let cellHeight = self.promotionListViewList[indexPath.row].tableViewHeight.value
if cellHeight < 0 {
return 1.0
} else {
return cellHeight
}
}

当我取消注释此方法并在 iPhone5 或 iPad2 上运行我的项目时,出现以下异常:

'table view row height must not be negative - provided height for index path'

然后应用程序崩溃。

但为什么呢?

我在这个方法中没有产生负值。

如果我评论它什么也不会发生。

最佳答案

如果您根据这个 stackoverflow 答案使用自动布局 https://stackoverflow.com/questions/18397206/autolayout-error-with-uitableview :

  1. Estimating row height of 1.0 results in an exception: "NSInternalInconsistencyException', reason: 'table view row height must not be negative...'"

  2. Estimating less than 1.0 has strange results as well.

  3. Estimating between 2.0 and the actual works just fine without any exceptions (although 2.0 is probably a bad choice for optimization reasons - you want to be as close to actual as possible).

  4. The constraint exception only occurs when the estimated row height is greater than the actual row height. The closer you are to the actual, the less likely the exception will occur. How far off your estimate can be before getting the exception seems to be related to several different factors: actual row height, the position of the row, etc.

这个解决方案应该避免异常:

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
let cellHeight = self.promotionListViewList[indexPath.row].tableViewHeight.value
if cellHeight < 0 {
return 1.01
} else {
return cellHeight
}
}

关于ios - UITableView 在我实现 `estimatedHeightForRowAt indexPath` 后崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49507290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com