I have a collectionView with multiple cells. Each cell is of this class:
class OuterCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var stackView: UIStackView!
override init(frame: CGRect) {
super.init(frame: frame)
print("initting")
for i in 1...30 {
let view = UIView()
view.backgroundColor = UIColor.red
view.tag = i
self.stackView.addSubview(view)
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
I am creating each cell which I would hope calls its initializer like so:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "outerCell", for: indexPath) as! OuterCollectionViewCell
cell.backgroundColor = UIColor.gray
return cell
}
My intent is to add 30 subviews to this horizontal stackView such that 30 stripes show up. I want to add these subviews in programmatically in the init to avoid having to do it manually in the storyboard. However right now the init is not called and the views are not added. Any idea why this might be and how to do this?