swift - 重新排序自定义单元格会导致约束混乱
问题描述
我有一个自定义单元格,它有一个看起来像这样的重新排序问题:
每当我尝试更改单元格的位置时,就会发生奇怪的事情,其中约束中断并且一半的单元格离开屏幕,然后当我退出编辑模式时,单元格在中间缩小为一个小矩形细胞曾经是。
这是我尝试重新排序单元格时出现的控制台消息。
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
}
}
}
解决方案
推荐阅读
- c++ - OpenCV DNN 不使用 Deeplabv3.onnx 模型产生预期结果
- kubernetes - 某些 Prometheus 警报最终显示为“未分组”
- android - com.google.android.apps.gsa.tasks.m EXCLUSIVE 后台任务 UPDATE_HOTWORD_MODELS 崩溃
- html - 选择嵌套 div 中的表单元素
- azure-devops - 通过 devops 管道发布新版本时无法使用 clickonce 应用程序
- c# - 如何在任何情况下按空格分隔字符串?
- javascript - For 循环不超过循环中的第一天
- jmeter - Jmeter - 如何获取所有时间戳的最小值(数组的最小值)
- excel - 数据透视表 - x 轴对数刻度
- module - 我可以在不重新创建 .mod 文件的情况下编译 f90