22

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

Brabbeldas
  • 1,939
  • 4
  • 20
  • 32

2 Answers2

19

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.

enreas
  • 11,152
  • 3
  • 31
  • 32
  • Row height can be set in many places. Where are you trying to set it? – enreas Feb 26 '14 at 13:03
  • I mean in Storyboard custom height for prototype cell is not working when I use dynamics prototype. From I know it's common know. – Błażej Feb 26 '14 at 14:02
16

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Hi Martin, thanks for clarifying the heightForRowAtIndexPath method. But this does not explain why this property is available if it is not being used. – Brabbeldas Aug 22 '12 at 18:34
  • @Brabbeldas: Hi! The height property is in fact used to create the cell. I have just verified that `dequeueReusableCellWithIdentifier` returns a cell with the height specified in the Size Inspector. But the tableview does not use the cell's height for positioning the cells. It uses only the value from `heightForRowAtIndexPath`. – Martin R Aug 22 '12 at 18:53
  • do you mean that the property is only used in design-time? – Brabbeldas Aug 22 '12 at 19:02
  • 6
    No, the cells do really have the height that you specified. But the cells's height is not used for *positioning* the cell. So if the cell's height is larger than what `heightForRowAtIndexPath` returns, then the cell will overlap with the next cell. – Martin R Aug 22 '12 at 19:04
  • Now that is a clear answer! I now do understand what you are saying! ..should I have been able to find this in the documentation? – Brabbeldas Aug 22 '12 at 19:10
  • That would be a separate question :-) – Martin R Aug 22 '12 at 19:13
  • What you may be able to do is pre-fetch a few of you custom height cells before-hand, and return their height in the delegate methods... It would be really handy if storyboards could do the job itself... – Rafael Nobre Oct 01 '13 at 18:08