UITableView dynamic cell heights only correct after some scrolling

后端 未结 24 1227
误落风尘
误落风尘 2020-11-29 16:41

I have a UITableView with a custom UITableViewCell defined in a storyboard using auto layout. The cell has several multiline UILabels.

24条回答
  •  失恋的感觉
    2020-11-29 17:04

    I have a similar problem, at the first load, the row height was not calculated but after some scrolling or go to another screen and i come back to this screen rows are calculated. At the first load my items are loaded from the internet and at the second load my items are loaded first from Core Data and reloaded from internet and i noticed that rows height are calculated at the reload from internet. So i noticed that when tableView.reloadData() is called during segue animation (same problem with push and present segue), row height was not calculated. So i hidden the tableview at the view initialization and put an activity loader to prevent an ugly effect to the user and i call tableView.reloadData after 300ms and now the problem is solved. I think it's a UIKit bug but this workaround make the trick.

    I put theses lines (Swift 3.0) in my item load completion handler

    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300), execute: {
            self.tableView.isHidden = false
            self.loader.stopAnimating()
            self.tableView.reloadData()
        })
    

    This explain why for some people, put a reloadData in layoutSubviews solve the issue

提交回复
热议问题