gpt4 book ai didi

swift - 无法删除 NSLAyoutConstraint/使其处于非事件状态

转载 作者:行者123 更新时间:2023-11-30 10:25:36 25 4
gpt4 key购买 nike

我有一个显示用户评论的UitableView。在每个单元格中,我还嵌入了另一个 UITableView,用于显示对这些评论的回复(如果有)。当我按下“回复”按钮时,我想将此回复表的高度(默认为 0)更改为 1000,以显示此内部 UITableView 呈现的回复列表。我尝试停用此内部 UITableView 的默认高度约束(0),但没有成功,我不知道为什么。

我当前的应用程序: enter image description here

下面是这个外部 UITableView 的 UITableViewCell 的代码

class CommentTableViewCell: UITableViewCell {

var nameLabel: UILabel = {
let label = UILabel()
label.font = .boldSystemFont(ofSize: 11)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
var imgView: UIImageView = {
let view = UIImageView()
view.backgroundColor = UIColor.black
view.contentMode = .scaleAspectFit
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var replyLabel: UILabel = {
let label = UILabel()
label.text = "Reply"
label.textColor = UIColor.appGrayForButtonTitles
label.font = UIFont.systemFont(ofSize: 11)
label.isUserInteractionEnabled = true
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

var numRepliesLabel: UILabel = {
let label = UILabel()
label.font = .boldSystemFont(ofSize: 11)
label.textColor = UIColor.app0946BF
label.textAlignment = .right
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
var repliesTextLabel: UILabel = {
let label = UILabel()
label.text = "Replies"
label.font = .boldSystemFont(ofSize: 11)
label.textColor = UIColor.app0946BF
label.isUserInteractionEnabled = true
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
var timeReplyContainer: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var timeAgoLabel: UILabel = {
let label = UILabel()
label.text = "1 hr"
label.textColor = UIColor.appGrayLight
label.font = UIFont.systemFont(ofSize: 11)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
var timeTextLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor.appGrayLight
label.text = "ago"
label.font = UIFont.systemFont(ofSize: 11)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
var repliesContainer: UIView = {
let view = UIView()
view.backgroundColor = UIColor.yellow
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var cellIndexPath: IndexPath?
var commentLabel: LabelWithPadding!
var repliesTableView: RepliesToCommentsTableView!
var replyToCommentItems = [Comment]()
let repliesID = "repliesToCommentsID"
var numberOfRepliesToComment: Int = 0
fileprivate var oldConstraints = [NSLayoutConstraint]()
fileprivate var oldReplyTableHeightConstraints = [NSLayoutConstraint]()
fileprivate var totalCellHeight: CGFloat?

/** init is called before dataObject.didSet*/
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
print("CommentTableViewCell.INIT")
self.selectionStyle = .none
setupViews()
}


var dataObject: Comment?{
didSet{
if let data = dataObject{
guard
let image = data.imageOfCommentor,
let name = data.name,
let comment = data.comment,
let replies = data.repliesToComment else{return}
print("dataObject.didSet for :\(name)")
repliesTableView.sourceData = replies
repliesTableView.reloadData()

numberOfRepliesToComment = replies.count
numRepliesLabel.text = "\(replies.count)"
imgView.image = image
nameLabel.text = name
commentLabel.label.text = comment

/**Only show number of replies to comments if they exist*/
if numberOfRepliesToComment == 0{
numRepliesLabel.alpha = 0
repliesTextLabel.alpha = 0
}
}
}
}


override func updateConstraints() {
print("updateConstraints for : \(nameLabel.text!)")



super.updateConstraints()
}

override func layoutSubviews() {
super.layoutSubviews()
print("layoutSubviews for \(nameLabel.text!)")

}


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


let borderSpace: CGFloat = 10
let viewSpace: CGFloat = 5

private func setupViews(){
let c = contentView.safeAreaLayoutGuide

/**Create commentLabel*/
let padding = UIEdgeInsets.init(top: 3, left: 5, bottom: 3, right: 5)
commentLabel = LabelWithPadding.init(frame: .zero, with: padding)
commentLabel.translatesAutoresizingMaskIntoConstraints = false

/**Create reply to comments tableView*/
repliesTableView = RepliesToCommentsTableView.init(frame: .zero, style: .plain, sourceData: replyToCommentItems, cellid: repliesID)
repliesTableView.translatesAutoresizingMaskIntoConstraints = false
repliesTableView.isScrollEnabled = false
repliesTableView.backgroundColor = UIColor.green

timeReplyContainer.addSubview(timeAgoLabel)
timeReplyContainer.addSubview(timeTextLabel)
timeReplyContainer.addSubview(replyLabel)
timeReplyContainer.addSubview(numRepliesLabel)
timeReplyContainer.addSubview(repliesTextLabel)

contentView.addSubview(imgView)
contentView.addSubview(nameLabel)
contentView.addSubview(commentLabel)
contentView.addSubview(timeReplyContainer)
contentView.addSubview(repliesTableView)

commentLabel.label.font = UIFont.systemFont(ofSize: 12)
commentLabel.label.numberOfLines = 0
commentLabel.label.lineBreakMode = .byWordWrapping
commentLabel.layer.cornerRadius = 7
commentLabel.layer.masksToBounds = true
commentLabel.backgroundColor = UIColor.appGrayExtraLightGray


/**Layout constraints*/
NSLayoutConstraint.deactivate(oldConstraints)
NSLayoutConstraint.deactivate(oldReplyTableHeightConstraints)

let newConstraints = [
imgView.topAnchor.constraint(equalTo: c.topAnchor, constant: borderSpace),
imgView.leadingAnchor.constraint(equalTo: c.leadingAnchor, constant: borderSpace),
imgView.widthAnchor.constraint(equalTo: c.widthAnchor, multiplier: 0.08),
imgView.heightAnchor.constraint(equalTo: c.widthAnchor, multiplier: 0.08),

nameLabel.topAnchor.constraint(equalTo: c.topAnchor, constant: borderSpace),
nameLabel.leadingAnchor.constraint(equalTo: imgView.trailingAnchor, constant: 10),
nameLabel.trailingAnchor.constraint(equalTo: c.trailingAnchor, constant: -borderSpace),


commentLabel.topAnchor.constraint(equalTo: nameLabel.bottomAnchor, constant: viewSpace),
commentLabel.widthAnchor.constraint(equalTo: nameLabel.widthAnchor),
commentLabel.trailingAnchor.constraint(equalTo: c.trailingAnchor, constant: -borderSpace),

timeReplyContainer.topAnchor.constraint(equalTo: commentLabel.bottomAnchor, constant: borderSpace),
timeReplyContainer.heightAnchor.constraint(equalToConstant: 30),
timeReplyContainer.widthAnchor.constraint(equalTo: nameLabel.widthAnchor),
timeReplyContainer.trailingAnchor.constraint(equalTo: c.trailingAnchor, constant: -borderSpace),
timeReplyContainer.bottomAnchor.constraint(equalTo: repliesTableView.topAnchor, constant: -10),

timeAgoLabel.leadingAnchor.constraint(equalTo: timeReplyContainer.leadingAnchor, constant: 5),
timeAgoLabel.topAnchor.constraint(equalTo: timeReplyContainer.topAnchor),
timeAgoLabel.trailingAnchor.constraint(equalTo: timeTextLabel.leadingAnchor, constant: -5),
timeAgoLabel.topAnchor.constraint(equalTo: timeTextLabel.topAnchor),

timeTextLabel.topAnchor.constraint(equalTo: timeReplyContainer.topAnchor),
timeTextLabel.trailingAnchor.constraint(equalTo: replyLabel.leadingAnchor, constant: -10),
replyLabel.topAnchor.constraint(equalTo: timeReplyContainer.topAnchor),

numRepliesLabel.topAnchor.constraint(equalTo: timeReplyContainer.topAnchor),
numRepliesLabel.trailingAnchor.constraint(equalTo: repliesTextLabel.leadingAnchor, constant: -5),
repliesTextLabel.topAnchor.constraint(equalTo: timeReplyContainer.topAnchor),
repliesTextLabel.trailingAnchor.constraint(equalTo: timeReplyContainer.trailingAnchor),

repliesTableView.widthAnchor.constraint(equalTo: nameLabel.widthAnchor),
repliesTableView.trailingAnchor.constraint(equalTo: c.trailingAnchor, constant: -borderSpace),
repliesTableView.bottomAnchor.constraint(equalTo: c.bottomAnchor, constant: -borderSpace)]


NSLayoutConstraint.activate([repliesTableView.heightAnchor.constraint(equalToConstant: 0)])
NSLayoutConstraint.activate(oldReplyTableHeightConstraints)
NSLayoutConstraint.activate(newConstraints)
oldConstraints = newConstraints
oldReplyTableHeightConstraints = [repliesTableView.heightAnchor.constraint(equalToConstant: 0)]

setupTapGestures()
}

private func setupTapGestures(){
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(showRepliesToComment))
self.repliesTextLabel.isUserInteractionEnabled = true
self.repliesTextLabel.addGestureRecognizer(tapGesture)
}

@objc func showRepliesToComment(){
print("show replies to comments")

self.oldReplyTableHeightConstraints[0].isActive = false
NSLayoutConstraint.deactivate(oldReplyTableHeightConstraints)

self.oldReplyTableHeightConstraints.remove(at: 0) print("showRepliesToComment.oldReplyTableHeightConstraints: (oldReplyTableHeightConstraints)") print("showRepliesToComment.oldReplyTableHeightConstraints.count: (oldReplyTableHeightConstraints.count)")

    let newConstraints = [self.repliesTableView.heightAnchor.constraint(equalToConstant: 1000)]
NSLayoutConstraint.activate(newConstraints)
self.oldReplyTableHeightConstraints = newConstraints
self.layoutIfNeeded()
}
}

控制台显示:

show replies to comments
showRepliesToComment.oldReplyTableHeightConstraints: []
showRepliesToComment.oldReplyTableHeightConstraints.count: 0
2020-02-05 09:04:36.468617+0000 Labuuk[8200:354387] [LayoutConstraints] Unable to
simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x600000425450 Labuuk.RepliesToCommentsTableView:0x7fdb13111000.height
== 0 (active)>",
"<NSLayoutConstraint:0x60000041cb40 Labuuk.RepliesToCommentsTableView:0x7fdb13111000.height
== 1000 (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60000041cb40 Labuuk.RepliesToCommentsTableView:0x7fdb13111000.height
== 1000 (active)>

从控制台可以看出,我已经明显停用了,然后手动删除了约束(出于绝望),然后停用了高度约束(为了更好的措施),但它仍然存在导致冲突!

最佳答案

我猜,问题是您在清除约束列表后将其停用。

因此尝试替换方法顶部的这一行showRepliesToComment:

NSLayoutConstraint.deactivate(oldReplyTableHeightConstraints)

因为现在您尝试停用一个空列表。

关于swift - 无法删除 NSLAyoutConstraint/使其处于非事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60072367/

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