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.
September 6th, 2009 at 19:57
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
September 18th, 2009 at 21:45
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.
September 21st, 2009 at 18:06
@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.
November 19th, 2009 at 11:07
Hi, thanks for this great tip.
Greetings alex