ios - estimatedHeightForRowAtIndexPath 不更新行高

标签 ios swift tableview

我有多个具有不同行高的动态单元格。因此,我尝试使用estimatedHeightForRowAtIndexPath 方法更新行高

这是我的代码:

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    switch indexPath.row {
    case 0:
        return 132
    case 1:
        return 66
    case 2:
        return 80
    case 3:
        return 70
    case 4:
        return 50
    case 5:
        return 60
    case 6:
        return 60
    default:
        return 60

    }

}

但是,行高不会更新。它使用 Storyboard 中的估计行高。有什么问题吗?

最佳答案

在您的情况下,您应该使用方法heightForRowAtIndexPath

当您想使用自动调整单元格大小时,请使用estimatedHeightForRowAtIndexPath

Providing an estimate the height of rows can improve the user experience when loading the table view. If the table contains variable height rows, it might be expensive to calculate all their heights and so lead to a longer load time. Using estimation allows you to defer some of the cost of geometry calculation from load time to scrolling time.

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{
   switch indexPath.row {
    case 0:
        return 132
    case 1:
        return 66
    case 2:
        return 80
    case 3:
        return 70
    case 4:
        return 50
    case 5:
        return 60
    case 6:
        return 60
    default:
        return 60

    }

}

关于ios - estimatedHeightForRowAtIndexPath 不更新行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39333134/

相关文章:

ios - 在 UIStackView 控件中调整 UIView 高度

iOS : Readjusting a UITableViewCell according the height of a UIImage in Swift

ios - Swift容易受到代码注入(inject)的攻击吗?

ios - 在自定义 .plist 文件中使用用户定义的build设置

ios - 在 Rx Swift 中自动将 UITableView 滚动到底部

ios - UIBezierpath 不响应中风

xcode - 如何从 xcassets 中抓取东西?

swift - 有没有办法在导航栏后退按钮上自定义辅助功能标签?

ios - 更新 imageView swift 4 的高度约束时无法同时满足约束

ios - Swift 3 - 在 TableView 中的 numberOfRowsInSection 后崩溃