gpt4 book ai didi

ios - 未计算 UITableViewCell 中的约束

转载 作者:行者123 更新时间:2023-11-29 11:35:37 28 4
gpt4 key购买 nike

我有以下单元格以编程方式设置约束:

class RadioButtonCell: UITableViewCell {

static let identifier = "RadioButtonCell"

let radioButton = RadioButton()
let labelTitle = UILabel()

private var didUpdateConstraints = false

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupSubViews()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupSubViews() {
radioButton.translatesAutoresizingMaskIntoConstraints = false
labelTitle.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(radioButton)
contentView.addSubview(labelTitle)
}

override func updateConstraints() {
super.updateConstraints()
if !didUpdateConstraints {
radioButton.anchor(leading: contentView.leadingAnchor, padding: UIEdgeInsets(top: 0, left: Constants.UI.defaultMarginX2, bottom: 0, right: 0))
radioButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
labelTitle.anchor(leading: radioButton.trailingAnchor, padding: UIEdgeInsets(top: 0, left: Constants.UI.defaultMarginX2, bottom: 0, right: 0))
labelTitle.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
didUpdateConstraints = true
}
}
}

anchor 方法只是一个添加约束的辅助方法。约束设置正确(Autolayout 没有问题)。

然后在 cellForRowAtIndexPath 方法中,我像这样创建单元格:

guard let cell = tableView.dequeueReusableCell(withIdentifier: RadioButtonCell.identifier, for: indexPath) as? RadioButtonCell else { return UITableViewCell() }
radtioButtonController.addButton(cell.radioButton)
cell.labelTitle.text = "test"
return cell

这在 tableView 中创建了以下布局(这显然是错误的): enter image description here

如果我将约束的设置移动到 setupSubViews() 方法,则布局是正确的:

class RadioButtonCell: UITableViewCell {

static let identifier = "RadioButtonCell"

let radioButton = RadioButton()
let labelTitle = UILabel()

private var didUpdateConstraints = false

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupSubViews()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupSubViews() {
radioButton.translatesAutoresizingMaskIntoConstraints = false
labelTitle.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(radioButton)
contentView.addSubview(labelTitle)
radioButton.anchor(leading: contentView.leadingAnchor, padding: UIEdgeInsets(top: 0, left: Constants.UI.defaultMarginX2, bottom: 0, right: 0))
radioButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
labelTitle.anchor(leading: radioButton.trailingAnchor, padding: UIEdgeInsets(top: 0, left: Constants.UI.defaultMarginX2, bottom: 0, right: 0))
labelTitle.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
}
}

enter image description here

为什么会这样?我认为我们应该在 updateConstraints 方法中设置约束...

谢谢你的回答:)

编辑 我发现,当我调用 cell.updateConstraintsIfNeeded()cell.setNeedsUpdateConstraints( )cellForRowAtIndexPath 中。为什么我们需要告诉单元格重新计算约束条件?当使用 IB 添加约束时,我们不需要这样做......

最佳答案

好吧,在您的第一个示例中,您不仅在 init 中将约束添加到您的 UI 元素。您只需设置您的 subview 。

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupSubViews()
// No explicit constraint setup happened
}

为了设置您的约束,您已经覆盖了 override func updateConstraints() { ... } 方法。我们来看看official documentation from Apple .

In short, your override will be in effect when you notify the system that you need constraints' update. As a result you need to explicitly inform the system by invoking setNeedsUpdateConstraints() or updateConstraintsIfNeeded().



让我们看看你的第二个例子。您在 private func setupSubViews(){ ... } 中嵌入了约束设置。因此,在调用此函数时,您的约束已准备好应用。无需系统调用。

关于ios - 未计算 UITableViewCell 中的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49528007/

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