【发布时间】:2018-11-28 17:09:12
【问题描述】:
我有一个带有自动维度高度和 2 个单元格的表格视图。 一个单元格有一个阅读更多按钮,用于更新标签的布局约束常量(如 IBOutlet)。它工作正常,单元格高度根据标签高度更新,但是当我第一次尝试在 cellForRowAt 方法委托上更新布局约束时它可以工作,但之后它会从阅读更多按钮停止工作(并且布局约束出口不会'不会变成零)。
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”行,阅读更多按钮可以工作,但我也需要在开头设置常量。
谢谢。
【问题讨论】:
-
每当你改变约束时尝试使用这个 cell.setNeedsUpdateConstraints() cell.updateConstraintsIfNeeded() cell.contentView.layoutIfNeeded()
-
谢谢,我试过了,但在这种情况下不起作用。
标签: swift autolayout