简体   繁体   中英

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

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. It fixed scrolling, but added another issue. Again on top of table view it works okay, but then... Watch gif please. Link for 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. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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