gpt4 book ai didi

ios - UITableViewCell 计算出正确的高度,但 UILabel 不换行

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:46 24 4
gpt4 key购买 nike

如果不设置明确的首选宽度,我无法让 UILabel 自动换行。请帮忙。

这是我在 UITableViewCell 中用于 UILabel 的界面生成器

UILabel IB Attributes Inspector UILabel IB Size Inspector

我的结果是:

Application Example Result

但是,如果我指定一个显式的 preferredWidth,如下所示:

explicit height

我得到以下信息:

With wordwrapping

虽然它有一些奇怪的垂直间距,但至少它可以正确换行。

此外,这是我在 UITableViewController 中构建行的方式:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {

let cell = tableView.dequeueReusableCellWithIdentifier(Constants.CellReuseIdentifier, forIndexPath: indexPath) as? BurnFeedTableViewCell ??
BurnFeedTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: Constants.CellReuseIdentifier)

cell.nameOfBurnLabel?.text = object.objectForKey("name") as? String
if let owner = object.objectForKey("owner") as? PFUser {
cell.ownerLabel?.text = owner.username
} else {
cell.ownerLabel?.text = "unknown"
}

return cell
}

并将 rowHeight 和 estimatedRowHeigh 定义为:

override func viewDidLoad() {
super.viewDidLoad()

self.tableView.estimatedRowHeight = 100.0
self.tableView.rowHeight = UITableViewAutomaticDimension
}

我真的很想在不必指定明确高度的情况下使它正常工作。另外,我玩过 contentCompressionResistancePriority,但运气不好。

这绝对类似于以下 SO 问题:Using iOS 8 flexible table cells, cell height changes but text doesn't wrap

但是,那里的解决方案似乎并不奏效。事实上,有几个类似的问题。不过,我似乎无法让它们工作。

编辑:来自 main.storyboard 的 tableViewCell 的 XML

<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Burn Feed Cell"
rowHeight="95" id="bif-hV-Vqt" customClass="BurnFeedTableViewCell" customModule="Burn"
customModuleProvider="target">
<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="bif-hV-Vqt" id="KGP-Sx-cZC">
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251"
verticalHuggingPriority="251" text="owner" lineBreakMode="tailTruncation"
baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO"
translatesAutoresizingMaskIntoConstraints="NO" id="TZ9-cx-K29">
<rect key="frame" x="8" y="37" width="48" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251"
verticalHuggingPriority="251" text="Burn Name" lineBreakMode="wordWrap" numberOfLines="0"
baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="90"
translatesAutoresizingMaskIntoConstraints="NO" id="v96-dx-9z5">
<rect key="frame" x="8" y="8" width="584" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251"
verticalHuggingPriority="251" text="countdown" lineBreakMode="tailTruncation"
baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO"
translatesAutoresizingMaskIntoConstraints="NO" id="6YZ-ne-79f">
<rect key="frame" x="506" y="37" width="86" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="v96-dx-9z5" firstAttribute="leading" secondItem="KGP-Sx-cZC"
secondAttribute="leadingMargin" id="Iwe-kT-1Do"/>
<constraint firstItem="6YZ-ne-79f" firstAttribute="trailing" secondItem="KGP-Sx-cZC"
secondAttribute="trailingMargin" id="JOA-d6-q3X"/>
<constraint firstItem="6YZ-ne-79f" firstAttribute="baseline" secondItem="TZ9-cx-K29"
secondAttribute="baseline" id="JXU-Hf-vVb"/>
<constraint firstItem="TZ9-cx-K29" firstAttribute="leading" secondItem="KGP-Sx-cZC"
secondAttribute="leadingMargin" id="VOG-Xh-9wd"/>
<constraint firstAttribute="bottomMargin" relation="greaterThanOrEqual" secondItem="TZ9-cx-K29"
secondAttribute="bottom" constant="5" id="am1-l0-8EV"/>
<constraint firstItem="TZ9-cx-K29" firstAttribute="top" secondItem="v96-dx-9z5" secondAttribute="bottom"
constant="8" symbolic="YES" id="jLQ-yQ-A8V"/>
<constraint firstItem="v96-dx-9z5" firstAttribute="trailing" secondItem="KGP-Sx-cZC"
secondAttribute="trailingMargin" id="sl2-Fb-sWy"/>
<constraint firstItem="v96-dx-9z5" firstAttribute="top" secondItem="KGP-Sx-cZC" secondAttribute="topMargin"
id="xt3-Em-EaP"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="countdownLabel" destination="6YZ-ne-79f" id="te1-QF-Nuo"/>
<outlet property="nameOfBurnLabel" destination="v96-dx-9z5" id="Wki-e1-0Np"/>
<outlet property="ownerLabel" destination="TZ9-cx-K29" id="wqh-Cj-yQX"/>
</connections>
</tableViewCell>

最佳答案

您的问题与自动布局有关。为了获得正确的宽度/高度换行,您需要确保 UITableViewCell 中的所有元素都从单元格的左边缘到右侧以及从上边缘到底部得到适当的约束。

关于ios - UITableViewCell 计算出正确的高度,但 UILabel 不换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28821990/

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