当我首先加载表视图时,两个标签在每个单元格中正确显示。然后,当我删除底部标签高度为零的项目时,以表观视图中的位置替换它的项目,然后具有零约束冲突和正确的高度。编译器始终选择零,使我的底部标签应该看到,消失了。我如何摆脱这种约束冲突,以便在这些情况下我的标签停止消失?
相关代码:在视图控制器中:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
guard let lineItem = orders[indexPath.section].lineItems?[indexPath.row] else { return 0 }
let width = CGFloat(UIScreen.main.bounds.width * 0.75)
guard let itemFont = UIFont(name: "ArialHebrew-Bold", size: 20),
let modifierFont = UIFont(name: "ArialHebrew-Light", size: 20) else { return 0 }
let heightForItemLabel = heightForView(text: "(lineItem.name) (x(lineItem.quantity))", font: itemFont, width: width)
let text = generateModifierText(lineItem)
let heightForModifierLabel = heightForView(text: text, font: modifierFont, width: width)
return heightForItemLabel + heightForModifierLabel + 20
}
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
private func generateModifierText(_ menuItem: MenuItem) -> String {
var text = ""
guard let modifiers = menuItem.modifiers else { return "" }
var optionNames = [String]()
for modifier in modifiers {
if !modifier.options.isEmpty {
for options in modifier.options{
if options.name.uppercased() != "NONE" {
optionNames.append(options.name)
}
}
}
}
for x in 0..<optionNames.count {
if x != optionNames.count - 1 {
text += "(optionNames[x]), "
} else {
text += "(optionNames[x])"
}
}
return text
}
然后在桌面单元格中:
func setup(_ lineItem: MenuItem) {
contentView.backgroundColor = .yellow
// Item Label
// itemLabel.backgroundColor = .white
itemLabel.text = "(lineItem.name) (x(lineItem.quantity))"
itemLabel.numberOfLines = 0
itemLabel.font = UIFont(name: "ArialHebrew-Bold", size: 20)
itemLabel.translatesAutoresizingMaskIntoConstraints = false
modifiersLabel.backgroundColor = UIColor.blue
// Modifiers Label
// modifiersLabel.backgroundColor = .white
modifiersLabel.numberOfLines = 0
modifiersLabel.text = generateModifierText(lineItem)
modifiersLabel.font = UIFont(name: "ArialHebrew-Light", size: 20)
modifiersLabel.translatesAutoresizingMaskIntoConstraints = false
modifiersLabel.backgroundColor = UIColor.red
// Add Labels to Content View
contentView.addSubview(itemLabel)
contentView.addSubview(modifiersLabel)
// Get Devie UI Size
let width = CGFloat(UIScreen.main.bounds.width * 0.75)
// Get Text from function based on lines
let text = generateModifierText(lineItem)
// Set heights
heightForModifierLabel = heightForView(text: text, font: modifiersLabel.font, width: width)
heightForItemLabel = heightForView(text: "(lineItem.name) (x(lineItem.quantity))", font: itemLabel.font, width: width)
// Set Contraints
let modifiersLabelConstrains = [
modifiersLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 50),
modifiersLabel.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.75),
modifiersLabel.heightAnchor.constraint(equalToConstant: heightForModifierLabel),
modifiersLabel.topAnchor.constraint(equalTo: itemLabel.bottomAnchor, constant: 4),
modifiersLabel.topAnchor.constraint(equalTo: itemLabel.bottomAnchor, constant: 4),
]
NSLayoutConstraint.activate(modifiersLabelConstrains)
let itemLabelConstrains = [
itemLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 50),
itemLabel.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.75),
itemLabel.heightAnchor.constraint(equalToConstant: heightForItemLabel),
itemLabel.widthAnchor.constraint(equalToConstant: 130),
itemLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 15),
]
NSLayoutConstraint.activate(itemLabelConstrains
}
override func prepareForReuse() {
modifiersLabel.text = ""
heightForItemLabel = 0
itemLabel.text = ""
heightForModifierLabel = 0
}
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
private func generateModifierText(_ menuItem: MenuItem) -> String {
var text = ""
guard let modifiers = menuItem.modifiers else { return "" }
var optionNames = [String]()
for modifier in modifiers {
if !modifier.options.isEmpty {
for options in modifier.options{
if options.name.uppercased() != "NONE" {
optionNames.append(options.name)
}
}
}
}
for x in 0..<optionNames.count {
if x != optionNames.count - 1 {
text += "(optionNames[x]), "
} else {
text += "(optionNames[x])"
}
}
return text
}
这不是解决方案。但是,请使用此代码更新您的问题,因为您的代码是不可理解的。我以某种方式更改了代码。不要个人。我们是来帮你的。我更改了每个标签的颜色。您只需要玩约束,并确保不会因已经锚定同一UI对象的另一个约束而崩溃。
func setup(_ lineItem: MenuItem) {
contentView.backgroundColor = .yellow
// Item Label
// itemLabel.backgroundColor = .white
itemLabel.text = "(lineItem.name) (x(lineItem.quantity))"
itemLabel.numberOfLines = 0
itemLabel.font = UIFont(name: "ArialHebrew-Bold", size: 20)
itemLabel.translatesAutoresizingMaskIntoConstraints = false
modifiersLabel.backgroundColor = UIColor.blue
// Modifiers Label
// modifiersLabel.backgroundColor = .white
modifiersLabel.numberOfLines = 0
modifiersLabel.text = generateModifierText(lineItem)
modifiersLabel.font = UIFont(name: "ArialHebrew-Light", size: 20)
modifiersLabel.translatesAutoresizingMaskIntoConstraints = false
modifiersLabel.backgroundColor = UIColor.red
// Add Labels to Content View
contentView.addSubview(itemLabel)
contentView.addSubview(modifiersLabel)
// Get Devie UI Size
let width = CGFloat(UIScreen.main.bounds.width * 0.75)
// Get Text from function based on lines
let text = generateModifierText(lineItem)
// Set heights
heightForModifierLabel = heightForView(text: text, font: modifiersLabel.font, width: width)
heightForItemLabel = heightForView(text: "(lineItem.name) (x(lineItem.quantity))", font: itemLabel.font, width: width)
// Set Contrains
let modifiersLabelConstrains = [
modifiersLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 50),
modifiersLabel.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.75),
modifiersLabel.heightAnchor.constraint(equalToConstant: heightForModifierLabel),
modifiersLabel.topAnchor.constraint(equalTo: itemLabel.bottomAnchor, constant: 4),
modifiersLabel.topAnchor.constraint(equalTo: itemLabel.bottomAnchor, constant: 4),
]
NSLayoutConstraint.activate(modifiersLabelConstrains)
let itemLabelConstrains = [
itemLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 50),
itemLabel.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.75),
itemLabel.heightAnchor.constraint(equalToConstant: heightForItemLabel),
itemLabel.widthAnchor.constraint(equalToConstant: 130),
itemLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 15),
]
NSLayoutConstraint.activate(itemLabelConstrains)
}
我需要做的解决问题不是使用高度约束,而是使用标签上的顶部和底部约束,并确保行的数量等于零。然后,我需要将估计的rowheight和rowheight设置为自动限度。仅此而已。