首页 > 解决方案 > 重新排序自定义单元格会导致约束混乱

问题描述

我有一个自定义单元格,它有一个看起来像这样的重新排序问题:

图像

每当我尝试更改单元格的位置时,就会发生奇怪的事情,其中​​约束中断并且一半的单元格离开屏幕,然后当我退出编辑模式时,单元格在中间缩小为一个小矩形细胞曾经是。

这是我尝试重新排序单元格时出现的控制台消息。

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. 
(
    "<NSLayoutConstraint:0x283a7cbe0 'Content View Leading to Shadow View Leading' H:|-(0)-[FriendZone.ShadowView:0x10512d210]   (active, names: '|':UITableViewCellContentView:0x105139d30 )>",
    "<NSLayoutConstraint:0x283a7cb90 'Content View Trailing to Shadow View Trailing' H:[FriendZone.ShadowView:0x10512d210]-(0)-|   (active, names: '|':UITableViewCellContentView:0x105139d30 )>",
    "<NSLayoutConstraint:0x283a7cb40 'Main Background Leading' H:|-(0)-[UIView:0x105139fb0]   (active, names: '|':FriendZone.ShadowView:0x10512d210 )>",
    "<NSLayoutConstraint:0x283a7caa0 'Main Background Trailing' H:[UIView:0x105139fb0]-(0)-|   (active, names: '|':FriendZone.ShadowView:0x10512d210 )>",
    "<NSLayoutConstraint:0x283a7c8c0 'Stack View Center X' UIStackView:0x10513a190.centerX == UIView:0x105139fb0.centerX   (active)>",
    "<NSLayoutConstraint:0x283a7c910 'Stack View Trailing' H:[UIStackView:0x10513a190]-(15)-|   (active, names: '|':UIView:0x105139fb0 )>",
    "<NSLayoutConstraint:0x283a7c780 'Time Width' UILabel:0x10513a880'9:48PM'.width <= 0.4*UIStackView:0x10513a190.width   (active)>",
    "<NSLayoutConstraint:0x283a79220 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x105139d30.width == 26.5   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283a7c780 'Time Width' UILabel:0x10513a880'9:48PM'.width <= 0.4*UIStackView:0x10513a190.width   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

我尝试寻找答案并调试我的约束,但我仍然无法解决问题,更不用说了解发生了什么。

这是单元格的视图层次结构及其约束。

图像

这是单元格的自定义类。

class PersonCell: UITableViewCell {
    @IBOutlet var name: UILabel!
    @IBOutlet var time: UILabel!
    @IBOutlet var mainBackground: UIView!
    @IBOutlet var shadowLayer: UIView!

    let gradientLayer = CAGradientLayer()

    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.frame = self.bounds
    }

    // To add left and right margins to tableViewCell and adjust gradientLayer's frame before the gradient is set
    override var frame: CGRect {
        get {
            return super.frame
        }
        set {
            var frame = newValue
            frame.origin.x += 5
            frame.size.width -= 2 * 5

            super.frame = frame
        }
    }
}

标签: swiftxcodeuitableviewautolayoutios-autolayout

解决方案


推荐阅读