Reputation: 611
I made a custom class from UITableViewCell
as MealItemDetailCell
.
In its xib, I dragged a UITableViewCell
and then added items to the cell. When populating the Table that displays the MealItemDetailCells
, the cells' bottoms are all cut off since they're larger than the table's default height.
I see where I can use heightForRowAtIndexPath
and set it to something large enough, but I might want to edit the items in the custom cell, and then this height might not be appropriate. Is there any way to programmatically retrieve the height of the TableViewCell
from the xib file?
In the controller, when I provide a cell for populating the table, I tried cell.frame.size.height
, but that seemed to return the default cell height of 45. cell.window.size.height
gave me the whole iPhone window.
My current xib cell is 110 high, seen from the Size Inspector when clicking on the TableViewCell
when looking at the MealItemDetailCell.xib
.
I would expect some attribute to be readable so that I can get this.
I searched for a while, but I couldn't find anything close. Any thoughts or guidance? Thanks.
Update (response to Henry Hong below): I see differing values in the Size Inspector. There's the "row height" value under the "Table View Cell" (top) section. Then there's a value in "Height" under the "View" section (under Show "Frame Rectangle"). The "row height" doesn't change when dragging the xib's table cell boundary until I uncheck and recheck the "custom" checkbox. Although, just trying this again, I see both follow the actual cell size when finishing dragging! And now, but not before, using the value incremented up/down arrows, I see the cell outline move as expected. But the "row height" still stays the same. I'm sure this is documented somewhere, but I guess I just have to get the "row height" number matching the actual/final edited xib cell height.
Upvotes: 2
Views: 1026
Reputation: 4674
Most of the provided answers cover your question. As far as I can see, the 'Row Height' in interface builder is just a convenience value, since editing the height directly via number is not allowed – only by dragging.
Interface builder writes this value into the xib data structure as 'rowHeight':
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" indentationWidth="10" shouldIndentWhileEditing="NO" rowHeight="65" id="3" customClass="PaperInfoCell">
However, despite parsing the nib yourself, it doesn't seem to be written in any property of the instance – hence your only solution that removes double bookkeeping is to instantiate the nib and get the row height from the cell's frame.
As a side note, all of that is less of a burden when using iOS8, auto layout, and UITableViewAutomaticDimension.
Upvotes: 0
Reputation: 445
I guess you can fix your problem implementing the UITableViewDelegate method tableview:HeightForRowAtIndexPath: and return the height for the cell that in your case is 110.
Cheers, Alessandro
Upvotes: 0
Reputation: 445
In interface builder click on the tableview and then in the ruler section set the height for the cell to 110 otherwise you get the standard height which is 44.
Hope it helps! Cheers
Upvotes: 1
Reputation: 196
cell.frame.size.height should be able to retrieve the height of the view from nib file.
Just make sure u have already assigned a value to the "row height" with size inspector in interface builder.
or it will return the default height.
Upvotes: 0