简体   繁体   English

iOS 11 UITableView删除行动画bug

[英]iOS 11 UITableView delete rows animation bug

video of the tableview animation bug tableview动画bug的视频

I have a table view which expands/collapse its cells. 我有一个表格视图,可以扩展/折叠其单元格。

As of iOS 11, the tableView starts to behave strangely on insertion and deletion of rows. 从iOS 11开始,tableView在插入和删除行时开始出现奇怪的行为。 The contentSize has changed before the animation block happens and consequently, in the video, you can see a buggy scroll back happening on collapsing cells. 在动画块发生之前,contentSize已经更改,因此,在视频中,您可以看到在折叠单元格上发生错误回滚。 The animation just looks wrong. 动画看起来不对劲。

This code worked perfectly on iOS 10. Do anyone know what has changed on Apple's side? 这段代码在iOS 10上完美运行。有人知道Apple方面有什么变化吗? Is this a known issue? 这是一个已知的问题?

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

I have been having countless problems with iOS 11 UITableView . 我在iOS 11 UITableView遇到了无数问题。 Going to every UITableView in my entire app and doing the following fixed all of my problems. 在我的整个应用程序中访问每个UITableView并执行以下操作修复了我的所有问题。

Set estimatedRowHeight , estimatedSectionHeaderHeight , and estimatedSectionFooterHeight to 0. estimatedRowHeightestimatedSectionHeaderHeightestimatedSectionFooterHeight为0。

Source: iOS 11 Floating TableView Header 来源: iOS 11浮动TableView标题

I had similar problem with table row removal animation on iOS 11 sometimes scrolling the table cells strangely (iOS 10 worked just fine). 我有类似的问题与iOS 11上的表行删除动画有时奇怪地滚动表格单元格(iOS 10工作正常)。 What helped was implementing this delegate method returning row height: 有助于实现此委托方法返回行高:

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

After that both iOS 10 and 11 work just fine. 之后iOS 10和11都运行良好。

In iOS 11.2, I had a bad animation after deleting a row using the standard row actions. 在iOS 11.2中,使用标准行操作删除行后,我的动画效果不佳。 I was only able to improve the situation by wrapping the row delete and row action dismissal in a CATransaction. 我只能通过在CATransaction中包装行删除和行动解除来改善这种情况。

I dismiss the row actions first and wait for that animation to complete before deleting the row from the table view. 我首先关闭行动作并等待该动画完成,然后从表视图中删除该行。

It at least doesn't jump around the table views content offset anymore, but is a lengthy two step animation. 它至少不会跳过表视图内容偏移,但是是一个冗长的两步动画。 I'm still looking for a better solution. 我还在寻找更好的解决方案。

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

I fixed it by using this code: 我使用此代码修复它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM