Even though @leonardloo's answer is partially correct and it may suit the needs of many people, I wanted to share how I fixed my issue.
I had a large tableView with big rows and had the same issue. When I've applied @leonardloo's answer, I've seen that it draws only number of visible cells it has found in the beginning and it draws 1 row whereas it's supposed to draw 5 (my datasource count).
To fix that, I have found such a simple solution:
UIView.animate(withDuration: 0, animations: {
self.tableView.layoutIfNeeded()
}) { (complete) in
var heightOfTableView: CGFloat = 0.0
// Get visible cells and sum up their heights
let cells = self.tableView.visibleCells
for cell in cells {
heightOfTableView += cell.frame.height
}
// Edit heightOfTableViewConstraint's constant to update height of table view
// Set tableView's constraint to height + 1 so that
// it can still draw the next visible cell it's supposed to.
self.heightOfTableViewConstraint.constant = heightOfTableView + 1
}
The point here is to let it draw the next cell so that it will be counted in visibleCells as well by setting constraint constant to height + 1. I've tested and it seems working like a charm now.