Ios Swift UITableViewCell未能在cellForRowAtIndexPath之后设置其约束

Ios Swift UITableViewCell未能在cellForRowAtIndexPath之后设置其约束,ios,swift,uitableview,nslayoutconstraint,Ios,Swift,Uitableview,Nslayoutconstraint,在我的tableViewController中,我使用一个自定义单元格,其中包含一个imageView、两个标签和一些其他不相关的元素。还有一个约束,如果给定某个条件,该约束的常量值应该改变。这在第一眼就可以很好地工作,但是如果我向下滚动并得到约束常数被设置为另一个值的单元格,那么下面的一些单元格会将该常数与前一个单元格保持相同,并且不会在出现时设置它 这是我的cellForRowAtIndexpath的相关部分: override func tableView(_ tableView: UIT

在我的tableViewController中,我使用一个自定义单元格,其中包含一个imageView、两个标签和一些其他不相关的元素。还有一个约束,如果给定某个条件,该约束的常量值应该改变。这在第一眼就可以很好地工作,但是如果我向下滚动并得到约束常数被设置为另一个值的单元格,那么下面的一些单元格会将该常数与前一个单元格保持相同,并且不会在出现时设置它

这是我的cellForRowAtIndexpath的相关部分:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let identifier = whichCellToShow ? "ThisCell" : "TheOtherCellCell"

    //...

    let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)

    if let customCell = cell as? CustomCellProtocol,
       indexPath.section < data.count,
       indexPath.row < data[indexPath.section].count {

        customCell.display(data: data[indexPath.section][indexPath.row])
    }

    return cell
}
如果
data.hasWeblink==true
约束常数应等于weblink标签的高度,如果为false,则应为0

我的第一个想法是,UITableView的视图循环可能会与titleIndentationConstraint.constant=data.hasWeblink冲突?weblink.frame.height:0。这就是为什么我随后直接调用了
setNeedsLayout()
layoutifneed()
,但这似乎没有帮助。单元格的所有其他元素都正确地执行其工作

此外,我还在tableViewController的
viewDidLoad()
内设置
tableView.rowHeight
tableView.estimatedRowHeight


有没有人知道哪里出了什么问题,或者我忘了做什么?谢谢转发

我自己修复了它,不得不更改标题标签的一些约束优先级:


我的标题标签有一个底部约束,它总是大于或等于8分。另外,我将行数限制为4行,并告诉标签如果文本太长而无法显示,则截断文本的尾部。有趣的是,如果文本被截断,我会得到上面描述的这种愚蠢行为,因此它与我的
标题indetantionconstraint
完全无关。如果文本必须被截断,则标签将自身与底部约束对齐,并将内容拖动到左中(因此请注意内容模式在属性编辑器中所说的内容,默认情况下应为左)。虽然更改标签的内容模式对我来说似乎是个坏主意,但我将标题标签底部约束的优先级降低了1,使其与图片顶部对齐,就像我计划的那样。希望这能帮助人们解决类似的问题B)。

您是否在某个地方停用了约束?@bseh nope,在调试器中也没有得到约束中断警告,但我得到了另一个热点,标题标签的行为似乎很愚蠢。您应该删除该问题
class CustomCell: UITableViewCell, CustomCellProtocol {
@IBOutlet weak var picture: UIImageView!
@IBOutlet weak var title: UILabel!
@IBOutlet weak var weblink: UILabel!
@IBOutlet weak var titleIndentationConstraint: NSLayoutConstraint!
//...

    func display(data: Data) {

        //...

        title.text = data.title

        //...

        weblink.text = data.hasWeblink ? URLUtilities.hostOfURL(urlstr: data.sourceUrl) : nil
        weblink.isHidden = !data.hasWeblink
        //This is the spot where things seem to go wrong
        titleIndentationConstraint.constant = data.hasWeblink ? weblink.frame.height : 0
        setNeedsLayout()
        layoutIfNeeded()
    }
}