gpt4 book ai didi

ios - 以编程方式向单元格 : cannot find its contentView 添加约束

转载 作者:行者123 更新时间:2023-11-28 06:21:23 25 4
gpt4 key购买 nike

我正在尝试向我的 UITableViewCell 中的 subview 添加约束,但出现错误 Unable to interpret '|'字符,因为相关 View 没有父 View

这是我自定义的 UITableViewCell 类:

class MessageTableViewCell: UITableViewCell {

var titleLabel: UILabel {
get {
let titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.font = UIFont.boldSystemFont(ofSize: self.defaultFontSize)

return titleLabel
}
}

var bodyLabel: UILabel {
get {
let bodyLabel = UILabel()
bodyLabel.translatesAutoresizingMaskIntoConstraints = false
bodyLabel.font = UIFont.systemFont(ofSize: self.defaultFontSize)

return bodyLabel
}
}
var thumbnailView: UIImageView {
get {
let thumbnailView = UIImageView()
thumbnailView.translatesAutoresizingMaskIntoConstraints = false
thumbnailView.layer.cornerRadius = CGFloat(TableViewCellArrangements.kMessageTableViewCellAvatarHeight/2.0)
thumbnailView.layer.masksToBounds = true

return thumbnailView
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)
configureSubviews()
}

private func configureSubviews() {

self.contentView.addSubview(thumbnailView)
self.contentView.addSubview(titleLabel)
self.contentView.addSubview(bodyLabel)

let views: [String : Any] = ["thumbnailView" : thumbnailView,
"titleLabel" : titleLabel,
"bodyLabel" : bodyLabel]
let metrics: [String : Any] = ["thumbSize" : TableViewCellArrangements.kMessageTableViewCellAvatarHeight,
"padding" : 15,
"right" : 10,
"left" : 5]
//The next line throws an exception. In fact, all the `let constraint` code will throw exceptions
let constraint1 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-left-[thumbnailView(thumbSize)]-right-[titleLabel(>=0)]-right-|", options: .alignAllLeft, metrics: metrics, views: views)
contentView.addConstraints(constraint1)
let constraint2 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-left-[thumbnailView(thumbSize)]-right-[bodyLabel(>=0)]-right-|", options: .alignAllLeft, metrics: metrics, views: views)
contentView.addConstraints(constraint2)
let constraint3 = NSLayoutConstraint.constraints(withVisualFormat: "V:|-right-[thumbnailView(thumbSize)]-(>=0)-|", options: .alignAllLeft, metrics: metrics, views: views)
contentView.addConstraints(constraint3)

if self.reuseIdentifier == TableViewCellArrangements.MessengerCellIdentifier {
let constraint4 = NSLayoutConstraint.constraints(withVisualFormat: "V:|-right-[titleLabel(20)]-left-[bodyLabel(>=0@999)]-left-|", options: .alignAllLeft, metrics: metrics, views: views)
contentView.addConstraints(constraint4)
} else {
let constraint5 = NSLayoutConstraint.constraints(withVisualFormat: "V:|[titleLabel]|", options: .alignAllLeft, metrics: metrics, views: views)
contentView.addConstraints(constraint5)
}
}

如上所述,约束线是抛出异常的地方。但是,我已经在设置约束之前将 subview 添加到 contentView,所以我很困惑为什么它认为有父 View 。

谢谢!

最佳答案

每次引用元素时,您都在重新创建元素的新实例。像这样尝试:

var xtitleLabel: UILabel?

var titleLabel: UILabel {
get {
if xtitleLabel == nil {
xtitleLabel = UILabel()
}
xtitleLabel?.translatesAutoresizingMaskIntoConstraints = false
xtitleLabel?.font = UIFont.boldSystemFont(ofSize: self.defaultFontSize)

return xtitleLabel!
}
}

您仍然在整个代码中引用 titleLabel - 这只会导致“创建一次”过程。

关于ios - 以编程方式向单元格 : cannot find its contentView 添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43329503/

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