Skip to content

Instantly share code, notes, and snippets.

@FahimF
Last active December 13, 2022 00:52
Show Gist options
  • Save FahimF/bf96e6978d45461187634f0fe1288183 to your computer and use it in GitHub Desktop.
Save FahimF/bf96e6978d45461187634f0fe1288183 to your computer and use it in GitHub Desktop.
A UICollectionView implementation of UIListContentConfiguration
import UIKit
class SimpleCollectionView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
private var collectionView: UICollectionView!
private var cellRegistration: UICollectionView.CellRegistration<UICollectionViewListCell, Book>!
override func viewDidLoad() {
super.viewDidLoad()
// View title
title = "Books - Collection"
// Collection view layout
let config = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
let layout = UICollectionViewCompositionalLayout.list(using: config)
// Add collection view
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(collectionView)
collectionView.delegate = self
collectionView.dataSource = self
// Cell registration
cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Book> { cell, indexPath, book in
var content = cell.defaultContentConfiguration()
content.image = book.authType == .single ? UIImage(systemName: "person.fill") : UIImage(systemName: "person.2.fill")
content.text = book.title
content.secondaryText = book.author
cell.contentConfiguration = content
}
}
// MARK: - Collection View Data Source / Delegates
func numberOfSections(in collectionView: UICollectionView) -> Int {
return Book.sections.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Book.booksFor(section: section).count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let section = Book.sections[indexPath.section]
let arr = Book.books[section]!
let book = arr[indexPath.row]
let cell = collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: book)
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment