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

时间:2018-01-19 02:31:43

标签: ios uitableview animation uiviewanimation

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

 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
}

1 个答案:

答案 0 :(得分: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
}
相关问题