I'm currently building a generic form builder using a UICollectionView.
Each form element is a cell, and for checkboxes, I'm using a UIStackView inside the cell to display all the available options.
The problem is that each time I reuse the cell, even if I remove all the arrangedSubviews, they stay in the view with the new one.
The following code is a simplified version of what I'm doing:
class cell: UICollectionViewCell {
@IBOutlet weak var stackView: UIStackView!
func setup(options: [String]) {
for option in options {
let label = UILabel()
label.text = option
stackView.addArrangedSubview(label)
}
}
override func prepareForReuse() {
super.prepareForReuse()
optionsStackView.arrangedSubviews.forEach({ optionsStackView.removeArrangedSubview(view: $0) })
}
}
Instead of that, my current workaround is to hide() each arrangedSubview in prepareForReuse() instead of removing them, but I don't like that.