I have have a UITableView with cells of automatic dimension using auto-layout. In addition, all of the subviews of the cells are using auto-layout (programmatic constraints).
Since these cells and their corresponding subviews have uniquely different sizes, when they are reused, the constraints for the cell and subviews do not seem to invalidated and reset and this results in the following:
Demonstration of problem:
I have tried all sorts of things in the prepareForReuse function but I haven't been able to reset all constraints. How can this be achieved?
Something I tried:
override func prepareForReuse() {
super.prepareForReuse()
invalidateIntrinsicContentSize()
removeConstraints(constraints)
subviews.forEach( { $0.removeConstraints($0.constraints); $0.invalidateIntrinsicContentSize() })
setNeedsUpdateConstraints()
}
This sort of situation is why there are multiple reuse buckets. Use a different cell subclass / reuse string for each type of cell. That way, there is nothing to do: just set up the cell once and let autolayout take care of resizing when, say, the text is different or (for an image cell) the image is different.