ios - 加载 View 后,静态 TableView 中的单元格不会立即调整大小

标签 ios swift uitableview

我希望海报有一个固定的高度,其余内容应该动态调整大小。第 2 和第 3 部分有文本,其大小应动态决定,即应根据文本内容决定。

image

仅当屏幕至少滚动 2 次时,内容才会调整大小。这是我在heightforrowat和估计的heightforat中写的代码。

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == 0{
        return CGFloat(250)
    }
    else {
        return UITableViewAutomaticDimension
    }
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 0{
        return CGFloat(250)
    }
    else {
        return UITableViewAutomaticDimension
    }

}

最佳答案

一些事情:

  1. 你不应该使用 UITableViewAutomaticDimension作为 estimatedHeightForRowAt 的返回值你应该只返回一个固定值。

  2. 您还需要设置 numberOfLines到 0 和 lineBreakModeword wrap对于所有 UILabels具有动态高度。

  3. 确保您已为每个 UILabel 正确设置约束让他们正确调整大小。即对其 superview 的顶部、底部、前导和尾随约束

关于ios - 加载 View 后,静态 TableView 中的单元格不会立即调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41905836/

相关文章:

ios - removeAllObjects 崩溃应用程序

ios - 在 Swift 中打印当前文件的类名

ios - 你如何测试 iOS 框架

swift - 图像模糊

arrays - 你能有一个在索引 0 处没有元素的数组吗

ios - UITableViewController : programmatically autolayout bug on iOS 7

swift - (Swift) 核心数据移动行 - 无效更新错误

ios - 如何在 iOS 应用程序中重现 Adob​​e Lightroom 的高光和阴影效果

ios - "Calendar.current.isDate"日期参数是否需要使用同一时区的日期?

ios - UITableView 从不调用 tableView :shouldShowMenuForRowAtIndexPath: on its delegate