In my tableView Cell, I have an array of strings from API which I uses to determine the height of a stackview based on the amount of array elements. The array can have 3, 4 or X number of elements.
var pollDataInPollCell: PollModel? {
didSet{
if let pollOptions = pollDataInPollCell?.poll_detail{
// pollOptions = ["A", "B"] or ["A", "XS", "XSMAX"]
optionBtnStackView.heightAnchor.constraint(equalToConstant: CGFloat(30 * pollOptions.count).isActive = true
}
var optionBtnStackView: UIStackView = {
let sv = UIStackView()
sv.axis = .vertical
sv.distribution = .fillEqually
return sv
}()
Everything works great at launch. The stackView height for each row looks perfect and there is no constraint breaking. However, if I scroll up and down the tableView, my stackView height isn't the same anymore and I believe it is due to tableView dequeueReusableCell. My question is if it is possible to reset my stackView height in didSet so that it can render new height. I have tried this in didSet
optionBtnStackView.heightAnchor.constraint(equalToConstant: 0).isActive = true
optionBtnStackView.heightAnchor.constraint(equalToConstant: CGFloat(30 * pollOptions.count).isActive = true
or
optionBtnStackView.heightAnchor.constraint(equalToConstant: CGFloat(30 * pollOptions.count).isActive = false
optionBtnStackView.heightAnchor.constraint(equalToConstant: CGFloat(30 * pollOptions.count).isActive = true
but both don't solve my problem. Any suggestions? Thank you so much.