iOS 11 UITableView删除行动画bug

iOS 11 UITableView delete rows animation bug

video of the tableview animation bug

我有一个 table 视图,其中 expands/collapse 个单元格。

自 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 11 UITableView 时遇到了无数问题。转到我整个应用程序中的每个 UITableView 并执行以下操作解决了我所有的问题。

estimatedRowHeightestimatedSectionHeaderHeightestimatedSectionFooterHeight 设置为 0。

来源:iOS 11 Floating TableView Header

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

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

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

        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()

我用这段代码修复了它:

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

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

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

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

对我有用的部分修复正在设置 estimatedRowHeight 很多。

tableView.estimatedRowHeight = 1000