gpt4 book ai didi

swift - tableview 将标签高度设置为零和正确的值,然后选择保留零以使标签消失

转载 作者:行者123 更新时间:2023-11-30 10:44:33 25 4
gpt4 key购买 nike

当我第一次加载表格 View 时,两个标签在每个单元格中正确显示。然后,当我删除底部标签高度为零的项目时,根据表格 View 中的位置替换它的项目则具有零和正确高度的约束冲突。编译器总是选择零,使我应该看到的底部标签消失。如何摆脱这种约束冲突,以便我的标签在这些情况下不再消失?

相关代码:在 View Controller 中:

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
}

然后在表格 View 单元格中:

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)


}

关于swift - tableview 将标签高度设置为零和正确的值,然后选择保留零以使标签消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56030028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com