单元格在UITableView动画过程中消失 [英] Cells disappearing during animation of UITableView

查看:170
本文介绍了单元格在UITableView动画过程中消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UIViewController中有一个正常的UITableView,但已限制为全屏显示.该tableView具有自定义的交互式tableHeaderView. tableHeaderView的大小是动态的,并且可以自行扩展. headerView具有textField,并将根据textField是否具有焦点来更改其自身大小.

I have a normal UITableView in my UIViewController, constrained to fullscreen. This tableView has a custom interactive tableHeaderView. The tableHeaderView is dynamic in size, and expands on its own. The headerView has a textField, and will change its own size depending on wether the textField has focus or not.

问题在于,更改tableHeaderView的大小时,屏幕底部的单元格不一定总是正确设置动画效果. 我在动画块内的layoutIfNeeded()之后调用tableView.beginUpdates()tableView.endUpdates().必须这样做,否则单元格根本不会遵循动态大小.

The problem is that the cells at the bottom of the screen aren't always animating correctly when changing the size of the tableHeaderView. I am calling tableView.beginUpdates() and tableView.endUpdates() after layoutIfNeeded() inside my animation-block. This has to be done, or the cells won't follow the dynamic size at all.

我制作了这个gif.在动画过程中,专门查看底部的单元格.我已经大大降低了动画的速度,因此很容易看到问题.

I've made this gif. Look specifically at the bottom cells during the animation. I have slowed down the animation considerably, so it's easy to see the problem.

推测:在我看来,动画开始后,tableView就会调用cellForRow:indexPath:,并以某种方式找出动画后整个tableView处于什么状态,并删除不必要的单元格,即使动画尚未完成. 折叠标题时,使用相同的方法:最底部的单元格不会以与已加载的单元格相同的方式进行动画处理.它们使用不同的动画进行动画处理..

Speculation: It seems to me like the tableView calls for cellForRow:indexPath: as soon as the animation starts, and somehow finds out what state the entire tableView will be in after the animation, and removing the unnecessary cells, even though the animation has not yet completed. The same way, when collapsing the header: the bottommost cells are not animated in the same way as the already loaded cells. They are animated in with a different animation..

为什么会这样?这可以预防吗?

Why is this happening? Is this preventable?

动画代码

self.navigationController?.setNavigationBarHidden(isEditing, animated: true)

var frame = tableHeaderView.frame
frame.size.height = tableHeaderView.headerHeight(forEditing: isEditing)

UIView.animate(withDuration: 0.3, animations: {

    if isEditing{
        let point = CGPoint(x: 0, y: -self.topLayoutGuide.length)
        self.tableView.setContentOffset(point, animated: false)
    }    

    tableHeaderView.frame = frame
    tableHeaderView.layoutIfNeeded()
    self.view.layoutIfNeeded()
    self.tableView.beginUpdates()
    self.tableView.endUpdates()
}) { [weak self](completed:Bool) in
    //Do stuff
}

推荐答案

我刚刚从Apple开发人员技术支持小组收到关于此问题的回信,以下是他们的逐字答复(重点是我的话):

I just heard back from the Apple Developer Technical Support team regarding this issue, and here's their response verbatim (emphasis mine):

你好,尼古拉斯,

Hello Nicolas,

感谢您与Apple开发人员技术支持(DTS)联系.

Thank you for contacting Apple Developer Technical Support (DTS).

您描述的行为和所产生的限制是设计使然的.

如果您认为Apple应该考虑采用其他方法, 我们建议您提出有关以下方面的信息的增强请求: 该设计决策如何影响您,以及您希望完成的工作 不一样.

If you believe an alternative approach should be considered by Apple, we encourage you to file an enhancement request with information on how this design decision impacts you, and what you’d like to see done differently.

尽管不能保证行为会有所改变,但这是 确保您对此事有想法的最佳方法是 负责此决定的团队.

Although there is no promise that the behavior will be changed, it is the best way to ensure your thoughts on the matter are seen by the team responsible for the decision.

最好的问候,

Apple开发者技术支持

Apple Developer Technical Support

似乎我们只需要使用对tableView.setContentOffset(_:animated:)tableView.scrollToRow(at:at:animated:)的简单调用即可.

It seems we're just stuck with having to use a simple call to either tableView.setContentOffset(_:animated:) or tableView.scrollToRow(at:at:animated:).

这篇关于单元格在UITableView动画过程中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆