ios - 多次调用 estimatedHeightForRowAtIndexPath 导致不稳定的 uitableview

标签 ios swift uitableview ios-autolayout uitableviewautomaticdimension

我在一个几乎每个 uitableviewcell 都具有不同高度的应用程序中有一个要求。此外,许多单元格都有带文本的图像(1、2 或 3)。

之前我使用的是heightForRowAtIndexpath,但像往常一样,性能非常糟糕,感觉不稳定,

我按照以下步骤解决了这个问题,

  1. 实现了estimatedHeightForRowAtIndexPath
  2. 为后续调用 estimatedHeightForRowAtIndexPath 的计算高度使用缓存
  3. 在所有单元格中使用自动布局。

与以前的方法相比,性能要好得多, 但是,由于列表不稳定,estimatedHeightForRowAtIndexPath 随后被调用了很多次。

如果单元格号 N 被加载,estimatedHeightForRowAtIndexPath 被调用 N-1 次,如果我们在列表中走得更远,就会发生这种情况, 如果我们倒退它知道估计的高度。

请帮助我理解为什么会发生这种情况,以及我可以做些什么来提高性能并使滚动更流畅。

最佳答案

为了让表格平滑,我使用了这个策略..

  1. 每当填充 tableView 数据源时,我都会计算每个单元格的高度。我将每个单元格的高度存储在数组 cellHeights
  2. heightforRowAtIndexPath 中,我返回 cellHeights[indexPath.row]

由此产生的过渡总是 super 流畅:)

关于ios - 多次调用 estimatedHeightForRowAtIndexPath 导致不稳定的 uitableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34953530/

相关文章:

ios - 跟踪存在 dequeueReusableCellWithIdentifier 疑问的 UITableViewCell 指针

ios - 最后一个 UITableViewCell 中的 UICollectionView 和一致的滚动?如何?

ios - iOS 表格/列表中的粘性菜单

swift - 无法将按钮焦点移动到下一行

iphone - 将 imageview 项目拖动到 UITableViewCell 上

iOS locationManager - 汽车仪表延迟

ios - viewController.view 未添加到父 View Controller 内的自定义 View

ios - 未找到符号 - UITextFieldDelegate

Swift 将徽章添加到导航 barButtonItem 和 UIButton

iOS Mapkit 自定义图像看起来放大了 2 倍