以编程方式向单元格添加约束:无法找到其contentView

时间:2017-04-10 17:34:16

标签: ios swift uitableview

我正在尝试在UITableViewCell中为子视图添加约束,但收到错误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) } } 课程:

{{1}}

如上所述,约束行是抛出异常的地方。但是,在设置约束之前,我已经将子视图添加到contentView,因此我很困惑为什么它认为有superview。

谢谢!

1 个答案:

答案 0 :(得分:1)

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

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 - 这只会导致“一次创建”过程。