我正在尝试动态计算 UITableView 的高度。我想我可以在 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
上做总和,问题是这个方法只对前 X 个单元格调用并且不会'在您滚动之前,不会再次调用新设置。
我正在使用自动布局约束,如何准确计算 TableView ?
最佳答案
您可以调用下面的方法来获取 UILabel 的高度..
- (CGFloat)heightForWidth:(float)width AndFont:(UIFont *)font ForString:(NSString *) yourTextValue
{
CGSize size = CGSizeZero;
if (yourTextValue.length > 0) {
CGRect frame = [yourTextValue boundingRectWithSize:CGSizeMake(width, 999999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height + 1);
}
return size.height;
}
另一种设置单元格动态高度的方法
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
关于ios - 如何动态计算桌面高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36094712/