问题
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.
回答1:
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
.
来源:https://stackoverflow.com/questions/2999580/do-i-need-to-set-heightforrowatindexpath-if-i-am-using-a-custom-uitableviewcell