ios - 如何动态计算桌面高度?

标签 ios swift uitableview

我正在尝试动态计算 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/

相关文章:

ios - 在 iOS 8.x 中,字符在 WebVTT 中被破坏以用于字幕

iOS BLE 蓝牙 - 发送/接收十六进制数据

ios - 一个View上有两个UITableView

ios - 添加childViewController不包括背景颜色等基本设置

ios - Xcode - 在我的应用程序上启用 iTunes 文件共享,但它没有出现在 iTunes 的列表中?

ios - 按钮中的图像无法加载,我只得到一个蓝色方 block

ios - 为什么我的收藏 View 单元格不显示...?

swift - UICollectionView - 为什么不调用这些方法?

iphone - UITableView滚动问题

ios - UITableView 在 contentInset 之后不滚动