I want to call tableView.reloadData()
and be sure that after that table rows are rendered.
The problem is that not happening when I am setting datasource programmatically before that. Example:
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.reloadData()
print("after reloadData")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cellForRowAt")
return UITableViewCell()
}
}
Output is:
- print("after reloadData")
- print("cellForRowAt")
I expect it to print “cellForRowAt” first. Why is that happening?
Update:
It's silly but I thought reloadData()
was synchronous.
What did the trick for me is:
self.tableView.layoutIfNeezded()
Thank you and thx this guy.