Когда я впервые загружаю таблицу, две метки отображаются правильно в каждой ячейке.Затем, когда я удаляю элемент, который имеет ноль для высоты нижней метки, элемент, который заменяет его с точки зрения положения в табличном представлении, тогда имеет конфликт ограничений, равный нулю и правильной высоте.Компилятор всегда выбирает ноль, поэтому моя нижняя метка, которая должна быть видна, исчезает.Как мне избавиться от этого конфликта ограничений, чтобы моя метка перестала исчезать в этих сценариях?
Соответствующий код: В контроллере представления:
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
}