Sep 02

To change the height of the cells in a UITableView, use the property rowHeight. Or change the value in Interface Builder.

There is a method called heightForRowAtIndexPath in UITableViewDelegate, where you can also set the height. However this is NOT recommended. Apple’s release notes states the following about this method:

It is very, very expensive to customize row heights (via tableView:heightForRowAtIndexPath:).

It makes sense that having rows with different heights in your table will wreak havoc with the table view’s reuse of cells. But it also turns out that if you return the same value from this method for each row, you also suffer a significant performance penalty.

So just use the simple rowHeight property. It’s less code to type, and it’s significantly faster.

Thanks to Brent for the performance testing.

Addendum: If you really, really must have different row heights in your table. Then heightForRowAtIndexPath is the only way to achieve this. If your table only has a handful of rows then performance will not be a big issue. But if you have hundreds of rows, all with varying heights, then I would suggest looking at constructing the table using HTML in a web view instead.

written by Nick \\ tags: , ,

4 Responses to “The Right Way To Set The Height Of TableViewCells”

  1. alones Says:

    Thank you for good information. 🙂
    In fact, I was confused because tableView:heightForRowAtIndexPath was not sued in the latest sample code.
    And I re-wrote this post on my blog in Korean. http://alones.kr/1537

  2. Ferm Says:

    Since the rowHeight property is on the tableview and not the cell, I guess I still have to use heightForRowAtIndexPath when having rows with different heights.

  3. Nick Says:

    @Ferm: If the rows in your table have to have different heights, then heightForRowAtIndexPath is the only option. I’ve updated the blog post to clarify that.

  4. Alex Says:

    Hi, thanks for this great tip.

    Greetings alex

Leave a Reply