UICollectionViewCell没有共同的祖先错误(不重复)

时间:2017-08-30 18:47:33

标签: ios swift uicollectionview nslayoutconstraint

class ChatCollectionViewCell: UICollectionViewCell {

var chatView: UIView!
var chatTextView: UITextView!
var isTextFromCurrentUser: Bool = true
var chatViewWidth: CGFloat = 200

override init(frame: CGRect) {
    super.init(frame: frame)

    chatView = UIView()
    chatTextView = UITextView()
    contentView.addSubview(chatView)
    contentView.addSubview(chatTextView)

    setupViews()

}

override func layoutSubviews() {

    if isTextFromCurrentUser {
        chatView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 10).isActive = true
        chatTextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 15).isActive = true
        chatTextView.backgroundColor = .white
    } else {
        chatView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10).isActive = true
        chatTextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -15).isActive = true
        chatTextView.backgroundColor = UIColor(r: 157, g: 255, b: 164)
    }

    chatView.widthAnchor.constraint(equalToConstant: chatViewWidth).isActive = true
    chatView.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
    chatTextView.widthAnchor.constraint(equalTo: chatView.widthAnchor, constant: -10).isActive = true
    chatTextView.topAnchor.constraint(equalTo: chatView.topAnchor, constant: 5).isActive = true

    chatView.translatesAutoresizingMaskIntoConstraints = false
    chatTextView.translatesAutoresizingMaskIntoConstraints = false

}

func setupViews() {

    chatView.backgroundColor = .blue

    chatTextView.font = UIFont.systemFont(ofSize: 16)
    chatTextView.layer.cornerRadius = 9
    chatTextView.clipsToBounds = true
    chatTextView.isScrollEnabled = false

}

override func prepareForReuse() {
    super.prepareForReuse()
    chatView = nil
    chatTextView = nil

    chatView = UIView()
    chatTextView = UITextView()
}

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

}

(为了澄清,我在ViewController的cellForRow方法中设置了chatViewWidth和chatTextView.text属性)

现在,XCode给出的错误如下:“无法使用锚点激活约束,因为它们没有共同的祖先。约束或其锚点是否引用不同视图层次结构中的项目?”

我查看了有关StackOverflow上常见祖先错误的许多帖子,但没有一个解决方案解决了我的问题。

这非常令人困惑。 我尝试使用断点来分析错误,但程序在创建约14个左右的单元格后崩溃了。有时它可以工作,但是当我添加更多单元格时,它将开始崩溃。我不确定问题是什么 - 我的观点肯定是CollectionViewCell的儿童观点,对吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

这就是我会这样做的...好吧我会在Storyboard中设置所有内容并为widthConstraint设置一个Outlet ......但是为了获取你的代码,这应该可行。但它不是测试...... :)

class ChatCollectionViewCell: UICollectionViewCell {

    var chatView: UIView!
    var chatTextView: UITextView!
    var isTextFromCurrentUser: Bool = true {
        didSet {
            if isTextFromCurrentUser {
                NSLayoutConstraint.deactivate(rightAlignmentConstraints)
                NSLayoutConstraint.activate(leftAlignmentConstraints)
                chatTextView.backgroundColor = .white

            } else {
                NSLayoutConstraint.deactivate(leftAlignmentConstraints)
                NSLayoutConstraint.activate(rightAlignmentConstraints)

                chatTextView.backgroundColor = UIColor(r: 157, g: 255, b: 164)
            }
        }
    }
    var chatViewWidth: CGFloat = 200 {
        didSet {
            chatView.widthAnchor.constraint(equalToConstant: chatViewWidth).isActive = true
        }
    }

    private var leftAlignmentConstraints: [NSLayoutConstraint] = []
    private var rightAlignmentConstraints: [NSLayoutConstraint] = []

    override init(frame: CGRect) {
        super.init(frame: frame)

        setupViews()

    }

    func setupViews() {

        chatView = UIView()
        chatTextView = UITextView()
        contentView.addSubview(chatView)
        contentView.addSubview(chatTextView)

        chatView.translatesAutoresizingMaskIntoConstraints = false
        chatTextView.translatesAutoresizingMaskIntoConstraints = false

        chatView.backgroundColor = .blue

        chatTextView.font = UIFont.systemFont(ofSize: 16)
        chatTextView.layer.cornerRadius = 9
        chatTextView.clipsToBounds = true
        chatTextView.isScrollEnabled = false

        leftAlignmentConstraints = [
            chatView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 10),
            chatTextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 15)
        ]

        rightAlignmentConstraints = [
            chatView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10),
            chatTextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -15)
        ]

        chatView.widthAnchor.constraint(equalToConstant: chatViewWidth).isActive = true
        chatView.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
        chatTextView.widthAnchor.constraint(equalTo: chatView.widthAnchor, constant: -10).isActive = true
        chatTextView.topAnchor.constraint(equalTo: chatView.topAnchor, constant: 5).isActive = true

        NSLayoutConstraint.activate(leftAlignmentConstraints)
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        chatTextView.text = ""

    }

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

好运和快乐编码