Do I need to set heightForRowAtIndexPath if I am using a custom UITableViewCell?

送分小仙女□ 提交于 2019-12-22 06:55:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!