Hi,
So I basically want to load data from an remote API and I have set all the files up etc but I am having trouble on the data snapshot part of the collection view to load the products as a list view.
Please see the code below.
import UIKit
class ViewController: UIViewController {
enum Section {
case main
}
var collectionView: UICollectionView!
var dataSource: UICollectionViewDiffableDataSource<Section,Product>?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
navigationItem.title = "Products"
// configureHierarchy()
//configureDataSource()
collectionView.register(ListCell.self, forCellWithReuseIdentifier: "ListCell")
}
func configure<T: SelfConfiguringCell>(with product: Product, for indexPath: IndexPath) -> T {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ListCell", for: indexPath) as? T else {
fatalError("Unable to dequeue Cell")
}
cell.configure(with: product)
return cell
}
}
extension ViewController {
private func createLayout() -> UICollectionViewLayout {
let config = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
return UICollectionViewCompositionalLayout.list(using: config)
}
}
extension ViewController {
private func configureHierarchy() {
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout())
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(collectionView)
collectionView.delegate = self
}
private func configureDataSource() {
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Product> { (cell, indexPath, product) in
var content = cell.defaultContentConfiguration()
content.text = "\(product.name)"
cell.contentConfiguration = content
}
dataSource = UICollectionViewDiffableDataSource<Section, Product>(collectionView: collectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, identifier: Product) -> UICollectionViewCell? in
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: identifier)
}
//inital data
var snapshot = NSDiffableDataSourceSnapshot<Section, Product>()
snapshot.appendSections([.main])
snapshot.appendItems([Product], toSection: .main)
dataSource?.apply(snapshot, animatingDifferences: false)
}
}
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
}
}
My API doesn't have separate sections hence why I used an enum to define one but where am I going wrong appending items to the section?
Best, Imran