简体   繁体   English

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

[英]UITableView reload animation doesn't work properly

I have a table view which reloads it data with animation on button tap. 我有一个表格视图,可在点击按钮时通过动画重新加载数据。 It was working great until there was one label with text. 效果很好,直到有一个带有文本的标签为止。 I use this code to reload data with animation for one button (I have only one section): 我使用此代码为一个按钮重新加载动画数据(我只有一个部分):

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

and this for another button: 这是另一个按钮:

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

And on the top of table view it works okay, but in the middle or in the end it scrolling. 并且在表格视图的顶部可以正常工作,但在中间或最后滚动。 Link for gif - https://giphy.com/gifs/l0DAHUyzm7BMGnKrm/html5 gif的链接-https: //giphy.com/gifs/l0DAHUyzm7BMGnKrm/html5

Then I added this code for reloading with animation: 然后,我添加了以下代码以重新加载动画:

        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)

Same code for .right animation. .right动画的相同代码。 It fixed scrolling, but added another issue. 它修复了滚动,但增加了另一个问题。 Again on top of table view it works okay, but then... Watch gif please. 再次在表格视图上工作还可以,但是然后...请观看gif。 Link for gif - https://giphy.com/gifs/xT1R9Gfaa2po6dMf2U/html5 gif的链接-https: //giphy.com/gifs/xT1R9Gfaa2po6dMf2U/html5

I'm using test data to fill table view, not fetching or something else. 我正在使用测试数据填充表格视图,而不是获取其他内容。 Hope for help, thanks 希望有所帮助,谢谢

EDIT: I found that if I set standard cell height from code animation works nice, only in this case it works: 编辑:我发现如果我从代码动画设置标准单元格高度很好,仅在这种情况下,它可以工作:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 44.0
}

I found the problem. 我发现了问题。 The problem was in cell height, animation was starting for estimated cell height in 44.0 and my cell height is 117.0. 问题出在单元格高度,动画开始于44.0的估计单元格高度,而我的单元格高度是117.0。 So this code fixed my problem: 所以这段代码解决了我的问题:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 117.0
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 117.0
}

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

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