Monday, February 7, 2011

UITableView with Custom Cell Height

I'm writing an application, where startup view contains UITableView - which seemed pretty slow. Basic version is not too bad, but when using a custom UITableViewCell created from XIB, it became totally unusable.

Time to debug whether Interface Builder's XIB makes things slow!

When UITableView contains 46410 rows, it took 10+ seconds to see the first application screen. When I added a few NSLog calls, it got worse. Not sure how much, since I gave up after 30 minutes. Finns can be pretty stubborn, even when debugging :)

To make a long story short, the reason was custom cell height implemented as UITableViewDelegate method tableView:heightForRowAtIndexPath. When you create a table, it seems like this method is called for each and every cell in the whole table. In my case it was called 46410 times, I guess. Didn't count them all. The reason for all these calls is that someone needs to know the exact height of the full table, for scrolling purposes. After the initial calculation is done, table was reasonably fast again. XIB didn't seem to make any difference.

How to fix it? Since all my custom cells had always same height, I just set UITableView property rowHeight to common fixed value and removed tableView:heightForRowAtIndexPath. You should define that method only, when your cells have variable heights (done that, too).


  1. nice recommendation. i never thought to just set the rowHeight directly on the UITableView instead of having each UITableViewCell be responsible for setting delegating its own height

  2. That's what I thought, too - until delay caused by that became painfully clear :)