【问题标题】:tableview sets label height to zero and the correct value, then opts to keep the zero making the label disappeartableview 将标签高度设置为零和正确的值,然后选择保持零使标签消失
【发布时间】:2019-05-07 20:26:40
【问题描述】:

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

相关代码:在视图控制器中:

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
}

然后在tableview单元格中:

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
}

【问题讨论】:

    标签: swift uitableview constraints


    【解决方案1】:

    这不是解决方案。但请使用此代码更新您的问题,因为您的代码无法理解。我已经更改了代码以使其更有意义。不要把它当作私人的。我们是来帮你的。我改变了每个标签的颜色。您只需要使用约束并确保您不会因已经锚定相同 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)
    
    
    }
    

    【讨论】:

    • 问题是,当一个表格视图单元格没有底部标签的文本并因此将该高度设置为零时,当我删除该单元格的数据并重新加载表格视图时,下面的数据填充在其中,编译器说零高度和正确高度存在冲突。我不知道如何玩弄代码会改变这一点。当我尝试为标签着色时,底部标签在这种情况下消失了,这正是我想要防止的。
    • @michaeldebo 你能展示你想要实现的目标的模拟或屏幕截图,以便我可以在这里为你提供帮助..
    【解决方案2】:

    解决这个问题我需要做的不是使用高度约束,而是使用带有标签的顶部和底部约束,并确保行数等于零。然后我需要将estimatedRowHeight 和rowHeight 设置为automaticDimensions。就是这样。

    【讨论】:

      猜你喜欢
      • 2016-10-19
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 2018-02-28
      • 1970-01-01
      • 2015-02-03
      • 2017-11-23
      相关资源
      最近更新 更多