rudisdotnet Kanashimi ikari chikara ni kaete




UITableViews with (or without) Dynamic UITableViewCells and Wicked-fast Scrolling

23 Jan 2009
Others, most notably the Tweetie developer, have discussed this, but I wanted to share my two cents on fast scrolling in UITableViews with (or without) dynamic cell heights.

I've been working on an app (I can let the cat out of the bag soon) that has (amongst other items) three main UITableViews. The first has static-height UITableViewCells with just three UILabels and the second has static-height UITableViewCells three UILabels and one UIImageView. For those two, the following settings are pretty important (translate Interface Builder settings to iPhone SDK calls if you are working – as I am – purely in code):



The next item of utmost importance is doing all of the calculations for the content of the cells outside of tableView:cellForRowAtIndexPath:. When I was prototyping, I had a good portion of the transformation code in that method and have since moved it to the XML parsing routines. Since I was not able to set my cells to be opaque, limiting the number of "inner" UIViews was also high on the priority list. I did not need to background load the images in the cells, so I did not have to resort to using a subclass of UITableViewCell but I *did* implement a very rudimentary caching system which helped to speed things up a bit:



For the dynamic height cells, I setup an NSMutableDictionary called "heights" (the key is the row number) which caches the row heights of each cell since one of the UILabels can drastically vary in height. You need to do something like the following in tableView:heightForRowAtIndexPath::



and then explicitly set the UILabel's frame size each time in tableView:cellForRowAtIndexPath: and also retrieve said hight and calculate the number of rows in tableView:cellForRowAtIndexPath::



If your table cells get any more complex or you need to delay or thread the loading of the images, then you will need to implement a custom table cell. When writing code to see what goes on behind the scenes, it turns out that there are many, many, many calls to those two methods for each table view, so it is very important to make them as lightweight as possible.

















Tags:

Post new comment

The content of this field is kept private and will not be shown publicly.


Syndicate

Syndicate content