当 Cell 初始化器调用函数时
private func configureUI() {
[dateLabel, previewLabel].forEach {
contentsStackView.addArrangedSubview($0)
}
[titleLabel, contentsStackView].forEach {
mainStackView.addArrangedSubview($0)
}
contentView.addSubview(mainStackView)
}
private func setUpConstraints() {
NSLayoutConstraint.activate([
mainStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10),
mainStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -10),
mainStackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5),
mainStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5)
])
dateLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
previewLabel.setContentHuggingPriority(.init(1), for: .horizontal)
}
private func setUpAccessory() {
accessoryType = .disclosureIndicator
}
添加或删除单元格警告日志消息
2023-09-05 19:52:06.961611+0900 Diary[7318:185913] [LayoutConstraints] 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:0x600000118050 V:|-(5)-[UIStackView:0x13c70e940] (active, names: '|':UITableViewCellContentView:0x13c71acd0 )>",
"<NSLayoutConstraint:0x6000001180a0 UIStackView:0x13c70e940.bottom == UITableViewCellContentView:0x13c71acd0.bottom - 5 (active)>",
"<NSLayoutConstraint:0x60000011f9d0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x13c71acd0.height == 0 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000001180a0 UIStackView:0x13c70e940.bottom == UITableViewCellContentView:0x13c71acd0.bottom - 5 (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.
当你滑动TableViewCell时,会出现上述Log warning,而当你Delete并滚动屏幕时,似乎是Cell被重用时出现的警告。
由于TableView第一次暴露的时候没有任何警告,所以我不认为是cell内部的约束问题。我很困惑,因为警告消息被列为与约束相关的问题。
回答转发问题
删除
mainStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5)
相反,创建此约束,为其分配优先级 999,然后激活它。