gpt4 book ai didi

ios - UICollectionViewCell 无共同祖先错误(不重复)

转载 作者:行者123 更新时间:2023-11-30 12:11:46 24 4
gpt4 key购买 nike

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 给出的错误如下:“无法激活带有 anchor 的约束,因为它们没有共同的祖先。约束或其 anchor 是否引用不同 View 层次结构中的项目?”

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

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

谢谢!

最佳答案

这就是我要做的...基本上我会在 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")
}
}

祝你好运,编码愉快

关于ios - UICollectionViewCell 无共同祖先错误(不重复),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45967750/

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