gpt4 book ai didi

ios - TableView 中的自动布局问题

转载 作者:行者123 更新时间:2023-11-30 11:36:15 25 4
gpt4 key购买 nike

我通过以下代码为我的 UITableView 使用动态高度单元:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionsCell", for: indexPath) as! QuestionsCell

cell.label1.text = ""
cell.label2.text = ""
cell.label1.text = some text from array
cell.label2.text = some text from array

cell.label1.lineBreakMode = .byWordWrapping
cell.label1.numberOfLines = 0
cell.label1.sizeToFit()

return cell
}

然后,我将每个元素(顶部、底部、前导和尾随)的所有约束固定在 Storyboard 中。在 iPhone 上一切正常,但在 iPad 上,当我滚动 UITableView 时,就会发生这种情况(动态标签高度):

enter image description here

最佳答案

sizeToFit() 只会使 cell1 达到文本所需的大小。您还需要对表格 View 单元格使用自动高度大小调整。

在viewDidLoad中,

self.tableView.rowHeight = UITableViewAutomaticDimension

您还需要确保从粗体标签到“12 天前”标签之间存在垂直约束,以确保它们之间始终存在垂直空间,这样它们就不会重叠。

关于ios - TableView 中的自动布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49739704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com