我有一个UITableViewCell的子类,它使用AutoLayout并通过UITableView registerNib(nib: UINib, forCellReuseIdentifier: String)
和dequeueReusableCellWithIdentifier(identifier: String)
重用。单元格有两个按钮,我在layoutSubviews()
上设置了AutoLayout约束:
override func layoutSubviews()
{
super.layoutSubviews()
//Clear all constraints from the cell
self.contentView.removeConstraints(self.contentView.constraints())
self.oneButton.setTranslatesAutoresizingMaskIntoConstraints(false)
self.twoButton.setTranslatesAutoresizingMaskIntoConstraints(false)
let viewsDict : [NSObject: AnyObject] = ["oneButton" : self.oneButton, "twoButton": self.twoButton]
//Setting the constraints
self.contentView.addConstraint(NSLayoutConstraint(item: self.oneButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.twoButton, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0.0))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-25-[oneButton]-25-[twoButton]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[oneButton(40)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[twoButton(40)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
}
这仅适用于第一次实例化单元格。重复使用后,水平间距和尺寸确定中断。在玩完并设置断点之后,我发现在updateConstraints
之后调用了layoutSubviews
,并且只在第一次实例化单元格时,而不是在重用之后。
稍微摆弄一下后我尝试了这个:
override func updateConstraints()
{
super.updateConstraints()
self.hasUpdated = true
}
override func layoutSubviews()
{
super.layoutSubviews()
if (!self.hasUpdated)
{
//Clear all constraints from the cell
self.contentView.removeConstraints(self.contentView.constraints())
self.oneButton.setTranslatesAutoresizingMaskIntoConstraints(false)
self.twoButton.setTranslatesAutoresizingMaskIntoConstraints(false)
let viewsDict : [NSObject: AnyObject] = ["oneButton" : self.oneButton, "twoButton": self.twoButton]
//Setting the constraints
self.contentView.addConstraint(NSLayoutConstraint(item: self.oneButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.twoButton, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0.0))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-25-[oneButton]-25-[twoButton]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[oneButton(40)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[twoButton(40)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
}
}
现在它按预期工作,但我觉得我可能会禁止一些重用布局机制,因为我并不总是设置约束。有什么想法吗?
编辑:在Shai发表评论后,我认为removeConstraints
和setTranslatesAutoresizingMaskIntoConstraints
来电可能是罪魁祸首。我所做的就是停止覆盖layoutSubviews
并将其更改为:
override func awakeFromNib()
{
super.awakeFromNib()
self.contentView.removeConstraints(self.contentView.constraints())
self.oneButton.setTranslatesAutoresizingMaskIntoConstraints(false)
self.twoButton.setTranslatesAutoresizingMaskIntoConstraints(false)
// Initialization code
}
override func updateConstraints()
{
super.updateConstraints()
let viewsDict : [NSObject: AnyObject] = ["oneButton" : self.oneButton, "twoButton": self.twoButton]
//Setting the constraints
self.contentView.addConstraint(NSLayoutConstraint(item: self.oneButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.twoButton, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0.0))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-25-[oneButton]-25-[twoButton]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[oneButton(40)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[twoButton(40)]-25-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
}
现在我觉得不喜欢黑客重用机制......还有,对此有什么看法吗?