Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS: Pre-Calculation of Row Heights for a UITableView
    text
    copied!<p>I have been struggling with this for months in my project.</p> <p>Here's the deal: I have a UITableView with just 1 section, but the heights of the UITableViewCells differ. Each UITableViewCell represents an object, and new objects are constantly becoming available, and inserted into the UITableView. The only way to effectively discover the height of a new object which will be displayed in the table is to draw it once (run the code that draws the UITableViewCell and look at what the resulting height is).</p> <p>Ah, but there's the problem! The UITableView delegate method tableView:heightForRowAtIndexPath: is called BEFORE the cell is drawn for the first time! This makes sense: the table wants to position the cells before drawing them.</p> <p>I've been experimenting LOTS and can find only 2 options, both of which have major downsides:</p> <ol> <li>If the new object to be shown in the table has not been seen before, simply "guess" the height and resize the cell later (after the cell is drawn) as needed. This causes problems withs scrolling though, since when the user stops scrolling it might be the case that one or more cells need to be resized to the correct height, which creates a bad user experience as the table shifts around and re-organizes itself.</li> <li>Pre-Calculate the object height by "testing" the object before inserting it into the UITableView. When the new object becomes available, create a "dummy" UITableViewCell and draw it off-screen, thus calculating the height of the object and saving it for later. The problem with this is that, of course, we can only do the height testing on the main thread (since we need to draw), which causes major lag in the application.</li> </ol> <p>My solution thus far has been to use #2, but to try to alleviate the problem by making it so that the height-tests only happen every 1/4 of a second, which helps to "space out" the testing, so that the main thread is not locked too much. But, the solution is inelegant and still causes lag problems, especially on older devices. The lag is bad enough that I really need a better solution.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload