'UICollectionViewCells are not showing up

I am trying to create a simple UICollectionView with images inside but the images are not showing up, all i see is a black screen. The code builds perfectly and the UICollectionView is working as well, because if I change it' background color it changes.

This is my ViewController:

private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewLayout())

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    view.backgroundColor = .yellow
    collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: CollectionViewCell.identifier)
    
    collectionView.delegate = self
    collectionView.dataSource = self
    
    view.addSubview(collectionView)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    collectionView.frame = view.bounds
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 30
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.identifier, for: indexPath)
    
    return cell
}

And my collectionViewCell:

static let identifier = "CollectionViewCell"

private let imageView: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    return imageView
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    contentView.addSubview(imageView)
    
    let image = UIImage(named: "diamond-1")
    imageView.image = image
}

required init?(coder: NSCoder) {
    fatalError()
}

override func layoutSubviews() {
    super.layoutSubviews()
    imageView.frame = contentView.bounds
}

override func prepareForReuse() {
    super.prepareForReuse()
    imageView.image = nil
}


Solution 1:[1]

First change the collectionView from let to var when declare.

private var collectionView: UICollectionView!

Then set the layout of the collectionView using UICollectionViewFlowLayout and initialise it in viewDidLoad() method.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    view.backgroundColor = .yellow
    
    let width = (self.view.bounds.width-10)/2
    let layout = UICollectionViewFlowLayout()
    layout.itemSize = CGSize(width: width, height: width)
    layout.minimumInteritemSpacing = 10
    layout.minimumLineSpacing = 10
    layout.scrollDirection = .vertical
    collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    
    collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: CollectionViewCell.identifier)
    
    collectionView.delegate = self
    collectionView.dataSource = self
    
    view.addSubview(collectionView)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1