Q:

How to get height of first n cells in UITableVIew? 

Say I want to limit table view's height constraint to show only particular number of top cells, how can I get their total height?  

A:

Swift 5:
extension UITableView {

func height(of top: Int, in section: Int = 0) -> CGFloat {

    let count = min(top, numberOfRows(inSection: section))

    let height = (0..<count)
        .map { IndexPath(row: $0, section: section) }
        .map { self.rectForRow(at: $0).height }
        .reduce(0, +)

    return height
}
}

goes well with:
extension UITableView {

func reloadData(completion: @escaping () -> ()) {
    CATransaction.begin()
    CATransaction.setCompletionBlock(completion)
    reloadData()
    CATransaction.commit()
}
}

to update constraint after data is loaded.