gpt4 book ai didi

uitableview - iOS 8 UITableViewCell调整大小,输出“无法同时满足约束”

转载 作者:行者123 更新时间:2023-12-01 06:27:55 25 4
gpt4 key购买 nike

我在情节提要中创建了一个UITableViewCell,并使用自动布局来放置所有内容,以期让iOS 8为我自动确定单元格的高度。我的单元格有两个标签,其中一个是具有“自动”的“首选宽度”的多行标签。

作为参考,单元格的高度为208,但高度设置为“默认”。表格视图的行高设置为208。

鉴于确实加载,我运行以下代码:

self.tableView.estimatedRowHeight = 100;
self.tableView.rowHeight = UITableViewAutomaticDimension;

第一次显示单元格时,此方法工作正常,一切都正确显示。问题是,如果单元被重用,它将错误地拉伸东西。基于 this post,我注意到我可能需要在cellForIndexPath中运行以下代码:
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];

但是,当我添加该代码时,得到消息:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one
you don't want. Try this: (1) look at each constraint and try to figure
out which you don't expect; (2) find the code that added the unwanted
constraint or constraints and fix it. (Note: If you're seeing
NSAutoresizingMaskLayoutConstraints that you don't understand, refer
to the documentation for the UIView property
translatesAutoresizingMaskIntoConstraints)
(
[...]
"<NSLayoutConstraint:0x7fd108dd1ce0 UITableViewCellContentView:0x7fd108d03d00.height ==>"
)

在这种情况下,我注意到我的 <tableViewCell>没有 <rect>标记,这就是为什么它输出 height==的原因。 (有关下面的 <rect>标签的更多信息...)

问题
  • 是真的需要在cellForRow中调用setNeedsUpdateConstraints和updateConstraintsIfNeeded吗?如果是这样,那为什么呢?我没有以编程方式更新任何约束。我之前创建过的项目在UITableViewCells内部调整了多行标签的大小,而在没有该代码的情况下,它看起来非常完美。

  • <rect>标签的奇怪行为

    这似乎与 <rect>中有关 <tableViewCell>标签的怪异行为有关。具体来说,它可能存在也可能不存在,如果确实存在,则其高度可能会设置为怪异的值,这可能会引起问题。

    这是我注意到的一些奇怪的行为...

    1.从对象库中拖动全新的UITableViewController会创建一个单元,如下所示:
    <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="VVy-eG-5ZD">
    <autoresizingMask key="autoresizingMask"/>
    <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="VVy-eG-5ZD" id="o4d-7b-psa">
    <autoresizingMask key="autoresizingMask"/>
    </tableViewCellContentView>
    </tableViewCell>

    在“大小”检查器中显示“默认”作为行高。

    请注意,没有 <rect>标记。

    2.将UITableViewCell从“对象库”拖动到该表视图控制器中,将按如下所示创建它:
    <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="SxR-MB-xdu">
    <rect key="frame" x="0.0" y="0.0" width="600" height="44"/>
    <autoresizingMask key="autoresizingMask"/>
    <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="SxR-MB-xdu" id="JDB-Ys-OoE">
    <autoresizingMask key="autoresizingMask"/>
    </tableViewCellContentView>
    </tableViewCell>

    在“大小”检查器中显示“默认”作为行高。

    注意如何使用 <rect>标记,它选择高度为44。在“大小”检查器中,表视图的“行高”的默认值是44。

    3.在“表格视图”的“大小”检查器中将“行高”属性更改为100。

    单元格在视觉上变大,但是它们的xml保持不变。

    作为参考,这是TABLE VIEW的xml之前的样子:
    <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="qea-mf-mRA">
    <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>

    ...这是下面的样子:
    <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="100" sectionHeaderHeight="22" sectionFooterHeight="22" id="qea-mf-mRA">
    <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>

    唯一改变的是 rowHeight="100"中的 rowHeight="44"

    4.将另一个单元格从“对象库”拖到“表视图”中

    即使表格视图的“行高”现在为100,我们仍然在 <rect>标记中获得44的高度:
    <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="wcV-YB-1eG">
    <rect key="frame" x="0.0" y="0.0" width="600" height="44"/>
    <autoresizingMask key="autoresizingMask"/>
    <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="wcV-YB-1eG" id="xM8-zX-Nrh">
    <autoresizingMask key="autoresizingMask"/>
    </tableViewCellContentView>
    </tableViewCell>

    5.调整情节提要中单元的大小以使其在视觉上更小(至55)

    这会将新的 rowHeight="55"属性添加到 <tableViewCell>标记,但不影响 <rect>:
    <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" rowHeight="55" id="wcV-YB-1eG">
    <rect key="frame" x="0.0" y="0.0" width="600" height="44"/>
    <autoresizingMask key="autoresizingMask"/>
    <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="wcV-YB-1eG" id="xM8-zX-Nrh">
    <autoresizingMask key="autoresizingMask"/>
    </tableViewCellContentView>
    </tableViewCell>

    如果您在“大小”检查器中查看“行高”属性,它现在显示为55,而不是“默认”。

    6.创建一个全新的Master Detail Project

    这将创建一个 <rect>标签:
    <tableViewCell contentMode="scaleToFill" selectionStyle="blue" accessoryType="disclosureIndicator" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="Cell" textLabel="2pz-XF-uhl" style="IBUITableViewCellStyleDefault" id="m0d-ak-lc9">
    <rect key="frame" x="0.0" y="86" width="320" height="44"/>
    <autoresizingMask key="autoresizingMask"/>

    请注意,即使情节提要板使用尺寸类别,因此所有内容都处于600宽度,它仍选择320宽度。另外请注意,无论选择为iPhone还是Universal创建模板,我们都将获得相同的准确结果。

    摘要

    是否创建了 <rect>标签?
  • 创建UITableViewController:否
  • 从主从模板创建项目:YES
  • 从对象库中拖动UITableViewCell:是

  • 问题
  • rect标签应该存在吗?
  • 如果是这样,应该将高度设置为什么?
  • 是否可以通过Interface Builder添加/删除/修改rect标签,而无需直接接触xml?


  • 对于我的原始单元格,当我手动更改rect标签时会发生以下情况:

    答:如果我完全删除 <rect>标记

    输出:
    Unable to simultaneously satisfy constraints ...
    "<NSLayoutConstraint:0x7fb51430b220 UITableViewCellContentView:0x7fb514309340.height ==>"

    B.将 <rect>设置为较小的高度

    如果将值设置为1到65之间的任何值,则会收到以下错误消息。

    在此测试中,我使用了:
    <rect key="frame" x="0.0" y="0.0" width="600" height="44"/>

    输出:
    Unable to simultaneously satisfy constraints ...
    "<NSLayoutConstraint:0x7fe3104ce760 UITableViewCellContentView:0x7fe3104e00d0.height == 44>"

    C.将 <rect>设置为较大的高度

    如果我将rect设置为65以上,它将不会输出到控制台。

    在此测试中,我使用了:
    <rect key="frame" x="0.0" y="0.0" width="600" height="999"/>

    除了不输出约束警告之外,一切看起来都是正确的。

    问题
  • 我如何知道要在任何给定单元格上设置哪个值?


  • 我将创建一个示例项目,以很快显示这些问题。

    最佳答案

    完成动画后尝试添加以下内容:

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    [cell layoutIfNeeded];

    关于uitableview - iOS 8 UITableViewCell调整大小,输出“无法同时满足约束”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26170666/

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