Ios UITableViewCell高度不正确,带有动态UILabels和内容压缩/阻力

Ios UITableViewCell高度不正确,带有动态UILabels和内容压缩/阻力,ios,swift,autolayout,uitableview,Ios,Swift,Autolayout,Uitableview,基于Xcode 7.2.1、iOS 9.2的构建 我有一个具有自动高度的单元格的UITableView tableView.estimatedRowHeight = 80 tableView.rowHeight = UITableViewAutomaticDimension 单元格的布局如下所示: UITableViewCell contentView outerStackView - pinned to top, left, right, bottom edges of the co

基于Xcode 7.2.1、iOS 9.2的构建

我有一个具有自动高度的单元格的
UITableView

tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
单元格的布局如下所示:

UITableViewCell
  contentView
  outerStackView - pinned to top, left, right, bottom edges of the contentView with autolayout
    leftColumnStackView - 38% of width of outerStackView
       topStackView
         label UILabel
         value UILabel
       bottomStackView
         label UILabel
         value UILabel
    middleColumnStackView - 38% of width of outerStackView
      ...
    rightColumnStackView - rest of the width of outerStackView
      ...
单元格中有6个标签/值对--[label X:value]。每个标签/值对由2个
UILabels
组成,相应地设置其内容拥抱和内容压缩阻力属性,以提供所需的外观

label.numberOfLines = 0
label.textAlignment = NSTextAlignment.Left
label.setContentCompressionResistancePriority(1000, forAxis: .Horizontal)
label.setContentHuggingPriority(1000, forAxis: .Horizontal)

value.numberOfLines = 0
value.setContentCompressionResistancePriority(500, forAxis: .Horizontal)
value.setContentHuggingPriority(500, forAxis: .Horizontal)

// there are 6 stack views, each containing a label/value pair
// the stack views are .Horizontal alignment, and pinned to the 
// columns they sit in
stackView.addArrangedSubview(label)
stackView.addArrangedSubview(value)
但是,有时单元格高度会自行测量,而有时会添加一堆额外的空间(有趣的是,如果两个标签都没有添加内容压缩/拥抱,那么额外的空间总是等于空间)

以下是发生的不一致性:

有时候是对的:

有时这是错误的:

但是,当我从标签中移除内容压缩/阻力时,单元格的高度始终正确,但如下所示:

有人有过类似的经历吗?有什么建议吗?

想好了

这与单元格的
contentView
是如何使用AutoLayout定位的有关。显然,默认情况下,它没有正确地布局和计算其子视图的位置

在我的UITableViewCell子类中,我将以下内容添加到我的
init()

通过将
contentView
固定到单元格,该
UITableViewCell
的高度分配不一致的问题消失了,并且始终显示正确的高度


这导致
contentView
被拉伸到所有4条边,这确实会影响其他一些事情(例如
UITableView
将编辑切换到
true
时的动画),但现在我可以用其他方式解决这些问题,因为没有任何事情被认为是错误的或正在发生的,这是意想不到的了

您的约束看起来像什么?@beyowulf所以所有内容都位于堆栈视图中的堆栈视图中。1个外部堆栈视图->3列内部堆栈视图->每列内部,2个堆栈视图,每个视图内部都有标签/值。我可以对有关结构的问题添加一些修饰。您在哪里调用
label.numberOfLines=0 label.textAlignment=NSTextAlignment.Left label.setContentCompressionResistancePriority(1000,forAxis:.Horizontal)label.setContentHuggingPriority(1000,forAxis:.Horizontal)等等。
?我猜您是在cellForRowAtIndexPath中设置压缩和拥抱,它有时在自动布局引擎通过之前调用,有时在我尝试将其移动到UITableViewCell子类的awakeFromNib之后调用。@beyowulf,我是以编程方式创建单元格,而不是使用故事板。我正在单元格的
init()
中设置
compression
hugging
numberOfLines
等,并为我的
UITableView
使用
dequeuReusableCellWithIdentifier
。你认为有没有一种类似于awakeFromNib的编程单元创建方法?
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.leadingAnchor.constraintEqualToAnchor(leadingAnchor).active = true
contentView.trailingAnchor.constraintEqualToAnchor(trailingAnchor).active = true
contentView.topAnchor.constraintEqualToAnchor(topAnchor).active = true
contentView.bottomAnchor.constraintEqualToAnchor(bottomAnchor).active = true