根据内容自动调整 TableViewCell 的大小不起作用

Posted

技术标签:

【中文标题】根据内容自动调整 TableViewCell 的大小不起作用【英文标题】:Auto-Resizing TableViewCell based on content not working 【发布时间】:2019-05-04 12:13:28 【问题描述】:

我正在尝试根据单元格的内容自动调整 UITableViewCells 的大小;但是,行高根本没有改变,所以内容被剪掉了。

我有一个带有一个原型单元的 UITableViewController。其中有一个固定有 0 个顶部、底部、前导和尾随约束的 UI 视图。在该视图中,我添加了一个具有固定宽度和高度的 ImageView,在视图中居中并带有顶部约束。此外,还有一个带有顶部和底部约束的居中标签。 所以约束应该设置得很好。

另外,我添加了

tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 300

viewDidLoad()TableViewController

而不是得到预期的结果

屏幕如下所示:

以下是约束:

有什么想法吗?

编辑: 仅出于测试目的,我现在创建了一个新的原型单元格,并且只为其添加了一个标签,使用前导、尾随、顶部和底部约束将其固定,并将其 linesproperty 设置为 0 以启用 AutoResize。但是,再次没有发生任何事情,标签甚至没有将文本包装在其中。

有关情节提要和约束以及实际结果,请参见此处: https://imgur.com/a/Hq5frWp

我的 TableViewController 代码:

import UIKit

class FirstTableViewController: UITableViewController


    override func viewDidLoad() 

        super.viewDidLoad()

        tableView.rowHeight = UITableView.automaticDimension

        tableView.estimatedRowHeight = 300.0

    


    override func numberOfSections(in tableView: UITableView) -> Int 
        // #warning Incomplete implementation, return the number of sections
        return 1
    

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        // #warning Incomplete implementation, return the number of rows
        return 1
    


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
            let cellA = tableView.dequeueReusableCell(withIdentifier: "article", for: indexPath) as! ArticleTableViewCell
            cellA.articleText.text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus "
            return cellA

    

【问题讨论】:

为什么内容视图里面有一个额外的视图?有什么具体原因吗? 是的,稍后启用阴影和圆角 tableView.rowHeight = UITableView.automaticDimension 没有给出任何语法错误吗? @RevanthKausikan 不,这是自 swift 4.2 以来的旧 UITableViewAutomaticDimension 【参考方案1】:

在我看来,你的禁忌是完美的。

由于您尚未与我们共享您的代码,我将指出所有检查点。

首先,检查你是否正确使用了tableView cellForRowAt函数。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

        guard let cell = tableView.dequeueReusableCell(withIdentifier: "thatCellsIdentifier", for: indexPath) as? YourTableViewCell else
            fatalError()
        

        cell.label.text = "sample"
        cell.image.image = UIImage(named: "yourImage")
        return cell
    

编辑 现在在 TableViewController 的viewDidLoad() 中设置它

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300

或使用 TableViewController 的尺寸检查器来设置它。

(我在屏幕截图中将估计值保持为自动,忽略它)

【讨论】:

除了可选的处理,它看起来相同:i.imgur.com/g2NYyMr.png 有趣...我会回到这个。【参考方案2】:

检查你是否在标签上设置了前导和尾随约束到它的超级视图,只有水平中心约束是不够的。

[或者,您也可以选择在标签上设置不带高度的固定宽度。 不要设置标签的高度:请参阅下面的更新答案]

如果您希望它根据内容增长,您还需要将标签上的 numberOfLines 设置为 0。

这就是标签高度的计算方式。

我尝试了一个小演示,它对我来说效果很好。

更新

不要在标签上设置高度,因为它会固定其高度并且不会根据内容增长。

正如 Revanth 所建议的,宽度就足够了,您可以选择在标签上设置“宽度”或“前导和尾随”约束。

但要确保它的高度是灵活的,并且 numberOfLines = 0 以使其增长。

【讨论】:

你的第一行不是真的。如果给出必要的宽度和中心约束,就足够了。唯一的可能性是您可能会收到内容被剪辑的警告。 @RevanthKausikan 你是对的,我刚刚给出了最佳选择。无论如何都可以从标签中推断出高度。更新了答案 即使有您的建议(添加前导和尾随约束;删除固定高度),它仍然看起来像这样:i.imgur.com/SKiauMr.png @woaahmxtn 请给我看你所有约束的截图,也许我会尝试做同样的事情,看看会发生什么。同时显示与约束相关的控制台错误(如果有)【参考方案3】:

刚刚删除了那个 TableViewController 并从头开始。现在一切正常...

【讨论】:

以上是关于根据内容自动调整 TableViewCell 的大小不起作用的主要内容,如果未能解决你的问题,请参考以下文章

自动调整 TableViewCell 的高度以适应 iOS 6 中的 UIImageVIew

tableViewCell 的 ContentView 不能正确自动调整大小?

自定义 tableviewcell 动态高度未根据约束更新(以编程方式自动布局)

iOS7根据从API下载的图像高度动态调整TableViewCell的高度

我在自定义 TableViewCell 中有一个 UICollectionView,但我无法自动调整 CollectionView 的大小

如何根据 UITableViewCell 中的内容调整水平 UICollectionView 的高度