Conclusion | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429927-beginning-collection-views/lessons/8

Hey Pasan,
I can recreate everything in Swift environment and really works as you just told us. But when I put everything in a SwiftUI UIViewRepresentable, it stopped working.
My issue is, that if Iโ€™m using the compositional layout the UIViewRepresentable does not pick it up, but when I change the layout back to the old UICollectionViewFlowLayout it works like a charm.
Hereโ€™s my code:

import SwiftUI
import UIKit

enum Section {
    case main
}

class Cell: UICollectionViewCell {
    
    var label = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        label.translatesAutoresizingMaskIntoConstraints = false
        addSubview(label)
        label.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        label.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

struct CollectionView: UIViewRepresentable {
    typealias UIViewType = UICollectionView
    
    func makeUIView(context: Context) -> UICollectionView {
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: configureLayout())
//        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
        collectionView.backgroundColor = .white
        collectionView.register(Cell.self, forCellWithReuseIdentifier: "cell")
        
        let datasource = UICollectionViewDiffableDataSource<Section, Int>(collectionView: collectionView) { (cv, ip, num) -> UICollectionViewCell? in
            if let c = cv.dequeueReusableCell(withReuseIdentifier: "cell", for: ip) as? Cell {
                c.label.text = "\(num)"
                c.backgroundColor = .systemRed
                
                return c
            }
            return nil
        }
                
        configureDatasource(datasource: datasource)
        context.coordinator.datasource = datasource
        
        return collectionView
    }
    
    func configureDatasource(datasource: UICollectionViewDiffableDataSource<Section, Int>) {
                
        var currentSnapshot = NSDiffableDataSourceSnapshot<Section, Int>()
        currentSnapshot.appendSections([.main])
        currentSnapshot.appendItems(Array(1...499))
        datasource.apply(currentSnapshot)
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }
    
    class Coordinator: NSObject {
        var datasource: UICollectionViewDiffableDataSource<Section, Int>!
    }

    func configureLayout() -> UICollectionViewCompositionalLayout {
    let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0))
    let item = NSCollectionLayoutItem(layoutSize: itemSize)
    item.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2)
    
    let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(44))
    
    let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
    
    let section = NSCollectionLayoutSection(group: group)
    
    return UICollectionViewCompositionalLayout(section: section)
    }

    func updateUIView(_ uiView: UICollectionView, context: Context) {
    }
}

Do you have any idea why?
Thank you

@pasanpr Can you please help with this when you get a chance? Thank you - much appreciated! :]

Unfortunately I donโ€™t have much experience with SwiftUI. I will try and reproduce this myself and get back to you.