如何设置UITableViewCell的高度
我知道当UIKit呈现单元格时,它使用tableView:heightForRowAtIndexPath:
来计算高度。 我的问题是,如何以及何时得到实际的UITableViewCell
设置。 我想要构builddynamic单元格,并需要计算单元格内文本的位置。 我相信我可以只使用self.bounds和self.frame – 我只是在那些设置什么点好奇,即使使用dequeueReusableCellWithIdentifier
。
谢谢。
从tableView:cellForRowAtIndexPath:
返回单元格后,tableview设置一些属性,包括单元格的边界,然后调用tableView:willDisplayCell:forRowAtIndexPath:
该调用后的唯一修改是设置alpha / frame,即使只在animation行时。
鉴于此,如果您正在使用库存UITableViewCell
,则可以在tableView:willDisplayCell:forRowAtIndexPath:
布局文本tableView:willDisplayCell:forRowAtIndexPath:
并确保该单元格已具有正确的宽度/高度。 也就是说,如果你做的不仅仅是基础知识,你可能想考虑UITableViewCell
。 在你的子类中,你可以在layoutSubviews
布局你的字段,并确保你的单元格总是正确布局。
您可以使用heightForRowAtIndexPath
委托,如下所示:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat result; result = 120.0f; return result; }
上面的例子将设置在canEditRowAtIndexPath
委托中创build的任何单元格帽子。
您也可以使用区域和行值来确定哪个单元/区域设置了高度。
您可以扩展自定义单元格
- (void)drawRect:(CGRect)rect { //You need to do your calculation for [super drawRect:rect]; }
你需要做你的计算,使其出现在drawRect的单元格的正确位置。