我有一个UITableCell,它显示各种类型的动态内容。estimatedHeightForRowAtIndexPath最佳做法
有标签,可以是1个或2行,用各种高度textViews,图像与各种高度。
什么是估计高度为这些行的最好方法?我可以制作计算标签和文字浏览高度的函数。但是对于约束条件,我可以使出口和总和所有的高度常数?
我有一个UITableCell,它显示各种类型的动态内容。estimatedHeightForRowAtIndexPath最佳做法
有标签,可以是1个或2行,用各种高度textViews,图像与各种高度。
什么是估计高度为这些行的最好方法?我可以制作计算标签和文字浏览高度的函数。但是对于约束条件,我可以使出口和总和所有的高度常数?
如果您正确设置自动布局。你不必自己计算常数。你可以简单地设置两个属性(或做在表视图委托):
tableView.estimatedRowHeight = 80;
tableView.rowHeight = UITableViewAutomaticDimension
和OS将检查正确的单元格高度为您服务。
如果你有几个不同的细胞,使其工作顺利最好的办法是实行委托方法,而不是通过属性tableView.estimatedRowHeight
做到这一点,例如:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// 1. get table view cell for indexPath
// 2. check which cell type have you got
// 3. return as closed estimated value for the cell type as you possible can.
// Example
//if cell type is my cell A return 80
// else if cell is B return 120, etc
}
我有这个,但是我得到了“跳动/缓慢”的tableview,所以它不工作。我在cellForRowAtIndexPath中改变了很多约束和高度值,也许这就是原因? –
您估计的RowHeight值大约是您的细胞高度应该是多少?它必须尽可能靠近你指定的地方。尝试将其添加到委托方法而不是tableView。属性。 – Greg
我在我的TableView中有5种不同类型的单元格,都有不同的estimatedRowHeight,所以我怎么能为我的整个TableView设置一个值? –
也许它可以帮助
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSAttributedString *attrStr = [[NSAttributedString alloc] init];
height = [self calculateHeightForAttributeString:attrStr];
return height;
}
-(CGFloat)calculateHeightForAttributeString:(NSAttributedString *)attrStr {
CGRect frame;
frame = [attrStr boundingRectWithSize:CGSizeMake(labelMaxWidth, MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
context:nil];
return frame.size.height;
}
显示您在tableview中使用的表格。应用适当的限制条件并设置estimatedRowHeight可以使您的单元格根据其内容自动增长。 – Irfan