gpt4 book ai didi

ios - 在 UITableViewCell 上调用 `[UIView -systemLayoutSizeFittingSize:]` 总是失败

转载 作者:IT王子 更新时间:2023-10-29 08:05:11 26 4
gpt4 key购买 nike

我想为 UITableViewCells 使用自动布局。这些表格单元格具有动态高度(取决于文本长度)。

我正在使用 [UIView -systemLayoutSizeFittingSize:]计算适当的单元格高度(在 [UITableView -heightForRowAtIndexPath:] 中返回)但我不断得到以下结果:

  • 如果我传递 UILayoutFittingCompressedSize,我会得到 (0,0) 的 CGSize。

  • 如果我传递 UILayoutFittingExpandedSize,我的应用程序会因以下错误而崩溃:

    *** Assertion failure in -[NSISLinearExpression incrementConstant:], /SourceCache/Foundation_Sim/Foundation-1043.1/Layout.subproj/IncrementalSimplex/NSISLinearExpression.m:620

(我的猜测是这意味着某个数字是无限的。)

我的实现很简单。我计算每个对象的高度,然后缓存它:

MessageCell *cell = // allocate a new cell...

// set up the cell (assign text, images, etc)

CGSize size = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

self.cellHeight = size.height; // size always equals (0, 0)

我假设这是我设置的约束的问题,但是:

  • 如果我手动将 cellHeight 设置为一个较大的值,单元格看起来都很好,只是高度不对。
  • Interface Builder 没有给我关于不明确约束的警告
  • [cell hasAmbiguousLayout] 返回 NO
  • 除其他外,我的单元格有一个设置为 48x48 的图像,因此 (0, 0) 的大小不应满足所有限制条件。

有什么想法吗?

最佳答案

这对我有用(注意“contentView”)

CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

编辑:我回答了一个类似的问题并提供了一个完整的例子,在这里:

How to resize superview to fit all subviews with autolayout?

关于ios - 在 UITableViewCell 上调用 `[UIView -systemLayoutSizeFittingSize:]` 总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17576005/

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