在表视图中,可以通过自动计算单元格的高度来正确显示单元格。以下是一个示例的解决方法:
viewDidLoad方法中,注册表视图的单元格类:override func viewDidLoad() {
    super.viewDidLoad()
    // 注册单元格类
    tableView.register(YourTableViewCellClass.self, forCellReuseIdentifier: "CellIdentifier")
}
tableView(_:cellForRowAt:):func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as! YourTableViewCellClass
    // 配置单元格的内容
    return cell
}
tableView(_:estimatedHeightForRowAt:)和tableView(_:heightForRowAt:):func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100 // 估计的单元格高度
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension // 自动计算单元格高度
}
通过设置estimatedHeightForRowAt为一个大致的高度估计值,表视图会在滚动时快速显示单元格。然后,通过设置heightForRowAt为UITableView.automaticDimension,表视图会自动计算并正确显示单元格的高度。
这样,当单元格中的内容超过了估计的高度时,表视图会根据实际内容的高度自动调整单元格的高度,以确保内容的完整显示。
                    上一篇:表示图像到类别映射所需的位数