UITableViewCell: Custom Row Height for prototype cell
Asked Answered
P

2

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

Philoctetes answered 16/8, 2012 at 14:26 Comment(0)
P
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.

Place answered 16/8, 2012 at 20:39 Comment(7)
Hi Martin, thanks for clarifying the heightForRowAtIndexPath method. But this does not explain why this property is available if it is not being used.Philoctetes
@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.Place
do you mean that the property is only used in design-time?Philoctetes
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.Place
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?Philoctetes
That would be a separate question :-)Place
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...Alessandro
D
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.

Dissuasive answered 22/1, 2014 at 15:45 Comment(2)
Row height can be set in many places. Where are you trying to set it?Dissuasive
I mean in Storyboard custom height for prototype cell is not working when I use dynamics prototype. From I know it's common know.Monty
P
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.

Place answered 16/8, 2012 at 20:39 Comment(7)
Hi Martin, thanks for clarifying the heightForRowAtIndexPath method. But this does not explain why this property is available if it is not being used.Philoctetes
@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.Place
do you mean that the property is only used in design-time?Philoctetes
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.Place
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?Philoctetes
That would be a separate question :-)Place
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...Alessandro

© 2022 - 2024 — McMap. All rights reserved.