我有一个表格 View ,其中高度为automaticDimension,并且有2个单元格。 其中一个单元格有一个“阅读更多”按钮,该按钮可更新标签的布局约束常量(如 IBOutlet)。它工作正常,单元格高度根据标签高度更新,但是当我第一次尝试在 cellForRowAt 方法委托(delegate)上更新布局约束时,它可以工作,但之后它停止从“阅读更多”按钮工作(并且布局约束导出不工作)不会变成零)。
if cellType == .movie {
let movieCell = cell as! MovieDescriptionCell
movieCell.updateCellWith(movie: selectedMovie!)
movieCell.infoLabelHeight.constant = 200. ***(here is the problem, if I don't have this the read more button works)***
movieCell.readMorePressedBlock = { [weak self] (tag) in
guard let strongSelf = self else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(70), execute: {
strongSelf.tableView.reloadData()
})
}
}
@IBOutlet var infoLabelHeight: NSLayoutConstraint!
@IBAction func onReadMoreButton(button: UIButton) {
self.readMoreState = !self.readMoreState
setReadMoreImageFor(state: readMoreState)
let infoText = self.infoLabel.text
let expectedHeight = infoText?.height(withConstrainedWidth: infoLabelWidth.constant, font: self.infoTextFont) ?? self.infoDefaultHeight
self.infoLabelHeight.constant = self.readMoreState ? expectedHeight : self.infoDefaultHeight
readMorePressedBlock?(button.tag)
}
我怎样才能做到这一点?如果我删除“movieCell.infoLabelHeight.constant = 200”行,“阅读更多”按钮将起作用,但我也需要在开头设置常量。
谢谢。
最佳答案
我找到了一个解决方案,如果我在 awakeFromNib 方法中设置布局约束,则“阅读更多”按钮可以工作(可以再次更改约束)
override func awakeFromNib() {
super.awakeFromNib()
self.infoLabelHeight.constant = 200
}
关于swift - 为标签设置布局常量后,它停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53505382/