Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to set heightForRowAtIndexPath if I am using a custom UITableViewCell?

Do I need to set heightForRowAtIndexPath if I am using a custom UITableViewCell? In my NIB I have already set the cell height.

When I over-ride heightForRowAtIndexPath the contents of my cell don't appear, even though it is set to the height defined in the NIB.

If I don't over-ride heightForRowAtIndexPath the contents of the cell appear, but there is overflow since the default height is not large enough.

like image 580
Sheehan Alam Avatar asked Jun 08 '10 17:06

Sheehan Alam


1 Answers

If all your rows are the same height, then you can set the rowHeight property of your UITableView instead of implementing tableView:heightForRowAtIndexPath:. If you have rows of different heights, then you have to implement tableView:heightForRowAtIndexPath:.

Neither of these methods will automatically return the height of the cell you defined in your NIB, as they get called before you start constructing cells in tableView:cellForRowAtIndexPath:.

To use the height of the cell defined in your NIB, I recommend that you define a custom property of your controller called prototypeCell, which will hold a single cell that never gets displayed on your table. In tableView:heightForRowAtIndexPath:, check to see if prototypeCell is nil. If it is, initialize it from your NIB. Then return prototypeCell.frame.size.height.

like image 161
cduhn Avatar answered Nov 14 '22 21:11

cduhn