如果我使用自定义UITableViewCell,是否需要设置heightForRowAtIndexPath? [英] Do I need to set heightForRowAtIndexPath if I am using a custom UITableViewCell?
问题描述
如果我使用的是自定义UITableViewCell,是否需要设置heightForRowAtIndexPath?在我的NIB中,我已经设置了单元格高度。
Do I need to set heightForRowAtIndexPath if I am using a custom UITableViewCell? In my NIB I have already set the cell height.
当我覆盖heightForRowAtIndexPath时,我的单元格的内容不会出现,即使它被设置为高度在NIB中定义。
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.
如果我不覆盖heightForRowAtIndexPath,则会出现单元格内容,但由于默认高度不够大,因此存在溢出。 / p>
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.
推荐答案
如果你的所有行都是相同的高度,那么你可以设置 rowHeight
UITableView
的属性,而不是实现 tableView:heightForRowAtIndexPath:
。如果你有不同高度的行,那么你必须实现 tableView:heightForRowAtIndexPath:
。
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:
.
这两种方法都不是将自动返回您在NIB中定义的单元格的高度,因为它们在您开始构建 tableView:cellForRowAtIndexPath:
中的单元格之前被调用。
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:
.
要使用NIB中定义的单元格的高度,我建议您定义一个名为 prototypeCell
的控制器的自定义属性,该属性将包含从未在桌面上显示的单个单元格。在 tableView:heightForRowAtIndexPath:
中,检查 prototypeCell
是否为零。如果是,请从您的NIB初始化它。然后返回 prototypeCell.frame.size.height
。
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
.
这篇关于如果我使用自定义UITableViewCell,是否需要设置heightForRowAtIndexPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!