iosswiftuitableviewheightrow-height

UITableView has wrong content size when all the inner sizes are fixed


I have UITableView with one section (1 header view + 100 rows). Its vertical constraints are to top view's bottom and to superview's bottom (not bottom safe area).

I provided all the necessary heights manually with UITableViewDelegate:

If I sum all these heights i get ~6000px but tableView.contentSize.height returns ~4500 until I scroll to the bottom of the table. And this happens even with default cells.

I understand that I can calculate everything manually but why table has wrong content size if all its inner element heights are predefined?


Solution

  • Found a solution for my case (when all the table items' heights are predefined): https://developer.apple.com/forums/thread/81895

    It seems heightForRow delegate method determines the height of the row which is drawn/visible on the screen but is not properly involved in contentSize calculation.

    estimatedHeightForRowAt looks like the opposite method. It determines the height of the row for contentSize calculation but without of heightForRow visible rows have the default height.

    So it is necessary to implement both heightForRow and estimatedHeightForRowAt methods for this case.

    The same is for header/footer heights (if these items are displayed)