UITableView重新加载动画无法正常工作

问题描述 投票:0回答:1

我有一个表视图,可以通过按钮点击动画重新加载数据。它有效,直到有一个标签带有文字。我用这个代码用一个按钮动画重新加载数据(我只有一个部分):

 tableView.reloadSections(IndexSet(integersIn: 0..<tableView.numberOfSections), with: UITableViewRowAnimation.right)

这是另一个按钮:

 tableView.reloadSections(IndexSet(integersIn: 0..<tableView.numberOfSections), with: UITableViewRowAnimation.left)

在表格视图的顶部它可以正常工作,但在中间或最后滚动。链接gif - https://giphy.com/gifs/l0DAHUyzm7BMGnKrm/html5

然后我添加了这个代码用于重新加载动画:

        let offset = tableView.contentOffset 
        tableView.beginUpdates()
        tableView.reloadSections(IndexSet(integersIn: 0..<tableView.numberOfSections), with: UITableViewRowAnimation.left)
        tableView.endUpdates()
        tableView.layer.removeAllAnimations()
        tableView.setContentOffset(offset, animated: false)

.right动画的代码相同。它修复了滚动,但增加了另一个问题。再次在表格视图之上它可以正常工作,但随后...请看GIF。链接gif - https://giphy.com/gifs/xT1R9Gfaa2po6dMf2U/html5

我正在使用测试数据来填充表视图,而不是获取或其他东西。希望得到帮助,谢谢

编辑:我发现,如果我从代码动画设置标准单元格高度很好,只有在这种情况下它工作:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 44.0
}
ios uitableview animation uiviewanimation
1个回答
0
投票

我发现了这个问题。问题出在细胞高度上,动画开始时估计的细胞高度为44.0,细胞高度为117.0。所以这段代码解决了我的问题:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 117.0
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 117.0
}
© www.soinside.com 2019 - 2024. All rights reserved.