Precompute the height of a UITableView with dynamically sized cells

Howdy,

I have a UITableView which is embedded deeply (i.e. nested in other views) within another UIScrollView for reasons. It seems that in order to get this outer UIScrollView to scroll properly, I have to constrain the height of this UITableView ahead of time. My problem is that I can't figure out how to calculate this height quickly with dynamically sized cells (thus contentSize and related methods won't work).

The best method I have been able to come up with is to scroll to each cell individually and fetch their height ahead of time sometime during/after layout, which is less than ideal... I imagine I could also adjust the height as cells are being loaded, but that doesn't sound like good user experience (scroll view keeps growing, wouldn't it be choppy?).

On quick investigation, it seems that a regular (i.e. non-embedded) UITableView is able to get an accurate height of dynamically sized cells ahead of time (as seen by the scroll bar). I'm curious if this means that there is something I am missing?

I'm sure there is a lot left for me to learn. I will appreciate any help.

Thanks,

smkuehnhold

Answered by DTS Engineer in 742629022

You said:

I have to constrain the height of this UITableView ahead of time

Constrain to what? Are you saying that you want to set the height of the inner table view so that it doesn't need to scroll its contents, so that scrolling gestures always scroll the outer scroll view? Is the number of rows in the inner table view fixed, or does it change over time?

On quick investigation, it seems that a regular (i.e. non-embedded) UITableView is able to get an accurate height of dynamically sized cells ahead of time (as seen by the scroll bar). I'm curious if this means that there is something I am missing?

No, it doesn't get an accurate height in advance. It computes the actual height of cells that are initially visible, but uses the row height estimate for all the other cells. (It's an implementation detail which cells have their real height computed initially, but you get the general idea.) That's sufficient to make scrolling work. For example, if you scroll down into an area of un-calculated heights, the estimate heights allow the table view to figure out the row you meant.

Then, as you scroll, newly visible cells are laid out, and the scroll bar is adjusted dynamically.

Overall, I don't know that there's any good way to achieve what you want. You can try monitoring the inner table view's contentSize and adjusting its bounds to match, but I can't predict how satisfactory the result would be.

Accepted Answer

You said:

I have to constrain the height of this UITableView ahead of time

Constrain to what? Are you saying that you want to set the height of the inner table view so that it doesn't need to scroll its contents, so that scrolling gestures always scroll the outer scroll view? Is the number of rows in the inner table view fixed, or does it change over time?

On quick investigation, it seems that a regular (i.e. non-embedded) UITableView is able to get an accurate height of dynamically sized cells ahead of time (as seen by the scroll bar). I'm curious if this means that there is something I am missing?

No, it doesn't get an accurate height in advance. It computes the actual height of cells that are initially visible, but uses the row height estimate for all the other cells. (It's an implementation detail which cells have their real height computed initially, but you get the general idea.) That's sufficient to make scrolling work. For example, if you scroll down into an area of un-calculated heights, the estimate heights allow the table view to figure out the row you meant.

Then, as you scroll, newly visible cells are laid out, and the scroll bar is adjusted dynamically.

Overall, I don't know that there's any good way to achieve what you want. You can try monitoring the inner table view's contentSize and adjusting its bounds to match, but I can't predict how satisfactory the result would be.

Precompute the height of a UITableView with dynamically sized cells
 
 
Q