【问题标题】:iOS 11 UITableView delete rows animation bugiOS 11 UITableView 删除行动画错误
【发布时间】:2018-02-28 10:16:58
【问题描述】:

video of the tableview animation bug

我有一个表格视图,可以展开/折叠其单元格。

从 iOS 11 开始,tableView 在插入和删除行时开始出现奇怪的行为。 contentSize 在动画块发生之前发生了变化,因此,在视频中,您可以看到在折叠单元格上发生错误的回滚。动画看起来不对。

此代码在 iOS 10 上完美运行。有人知道 Apple 方面发生了什么变化吗?这是一个已知的问题?

public func insertingRowsForAccordion(_ indexArray: [IndexPath], selectedRowIndex: Int) {
    beginUpdates()
    insertRows(at: indexArray, with: UITableViewRowAnimation.fade)
    endUpdates()

 // Scroll to selection after expanding children
    scrollToRow(at: IndexPath(row: selectedRowIndex, section: 0), at: UITableViewScrollPosition.top, animated: true)
}

public func removeRowsForAccordion(_ indexArray: [IndexPath]) {
    beginUpdates()
    deleteRows(at: indexArray, with: UITableViewRowAnimation.fade)
    endUpdates()
}

【问题讨论】:

  • 可能无关紧要,但您不需要开始/结束更新。那是为了批量操作。

标签: ios swift uitableview ios11


【解决方案1】:

我在使用 iOS 11 UITableView 时遇到了无数问题。转到我整个应用程序中的每个UITableView 并执行以下操作解决了我的所有问题。

estimatedRowHeightestimatedSectionHeaderHeightestimatedSectionFooterHeight 设置为0。

来源:iOS 11 Floating TableView Header

【讨论】:

  • 这修复了一个可怕的错误,我一直试图修复在拖动重新排序操作期间在单元格周围移动会导致失败的毛刺、生涩动画。
  • 这还修复了另一个错误,即向视图控制器添加新表格视图会导致它在顶部无缘无故地获得额外的内容。
  • 这为我修复了一个 iOS 11 错误,其中 tableview 会在每次加载时向后滚动。谢谢。
  • 将estimatedRowHeight 单独设置为0 为我在iOS11(并且只有iOS11)中修复了一个奇怪的错误,在该错误中,将行重新加载到表格视图的底部(同时在屏幕之外)会导致细胞在最终到达它们的位置之前上下飞行。
  • 不,这里的解决方案没有帮助,它严重损坏并且看起来很糟糕。我正在使用具有自动布局和大小类的调整单元格大小。问题只发生在 tableview 的底部附近,可能是 table 的最后 20%。
【解决方案2】:

在 iOS 11.2 中,使用标准行操作删除一行后,我的动画效果不佳。我只能通过将行删除和行操作解除包装在 CATransaction 中来改善这种情况。

我首先关闭行操作并等待该动画完成,然后再从表格视图中删除该行。

它至少不再在表格视图内容偏移中跳转,而是一个冗长的两步动画。我仍在寻找更好的解决方案。

        CATransaction.begin()
        CATransaction.setCompletionBlock({
            self.tableView.beginUpdates()
            self.myViewModel?.items?.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.top)
            self.tableView.endUpdates()
        })
        self.tableView.setEditing(false, animated: true)
        CATransaction.commit()

【讨论】:

    【解决方案3】:

    iOS 11 上的表格行删除动画有时会奇怪地滚动表格单元格,我遇到了类似的问题(iOS 10 工作得很好)。实现这个返回行高的委托方法有帮助:

    - (CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

    之后,iOS 10 和 11 都可以正常工作。

    【讨论】:

      【解决方案4】:

      我使用以下代码修复了它:

      self.tableView.beginUpdates()
      // ...
      self.tableView.endUpdates()
      self.tableView.layer.removeAllAnimations()
      

      【讨论】:

        【解决方案5】:

        对我有用的部分修复是设置estimatedRowHeight 大量。

        tableView.estimatedRowHeight = 1000
        

        【讨论】: