Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell: Custom Row Height for prototype cell

Tags:

In my storyboard file I am designing multiple prototype cells in a UITableView. Each cell has its own unique Cell Identifier.

Depending on the section and the row I dequeue one of the prototype cells in method - tableView:cellForRowAtIndexPath:

For most of the prototype cells I have not changed the Row Height. On my storyboard, their height seems to be determined by the property 'Row Height' under 'Size Inspector' of UITableView.

For one prototype cell I have changed the height through the property 'Row Height' under 'Size Inspector' of the specific UITableViewCell. The checkbox 'Custom' is also checked.

On my storyboard this seem to work well. But during runtime when my cells are being dequeued and added to the TableView, all cells get the default row height.

Now, I am aware of the method tableView:heightForRowAtIndexPath: which is being mentioned in other posts but it seems a little odd to use as I am setting a custom row height in my storyboard already.

Does somebody know why this property is not working? Is this property maybe mend to be used in different situations?

Thanks

like image 515
Brabbeldas Avatar asked Aug 16 '12 14:08

Brabbeldas


2 Answers

If you want to use Interface Builder in Xcode 5 to change the height of the rows in a table view without code you can do it in two places:

  • Select the table view, show the Size inspector and type it in Row Height, in the Table View Size section.

Default row height

  • Select the prototype cell, show the Size inspector, check Custom in the Table View Size section and type the row height in the associate text field.

Custom row height

Could it be that you're using the second method? Try to use the first method to set the default height of all the rows in the table view and the second to tell Interface Builder about the exceptions.

like image 108
enreas Avatar answered Sep 28 '22 09:09

enreas


heightForRowAtIndexPath is called (for each row of the table) before the cells are displayed, i.e. before cellForRowAtIndexPath is called. That is necessary to compute the layout of the underlying scrollview, the scroll indicators etc.

like image 45
Martin R Avatar answered Sep 28 '22 10:09

Martin R