Skip to content

Instantly share code, notes, and snippets.

@ytyubox
Created April 7, 2021 07:31
Show Gist options
  • Save ytyubox/751cde569190482163e21c10f0e16b2c to your computer and use it in GitHub Desktop.
Save ytyubox/751cde569190482163e21c10f0e16b2c to your computer and use it in GitHub Desktop.
let containerCellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, OutlineItem> { cell, _, menuItem in
var contentConfiguration = cell.defaultContentConfiguration()
contentConfiguration.text = menuItem.title
if menuItem.imageName != nil {
contentConfiguration.image = UIImage(systemName: menuItem.imageName!)
}
contentConfiguration.textProperties.font = .preferredFont(forTextStyle: .headline)
cell.contentConfiguration = contentConfiguration
let disclosureOptions = UICellAccessory.OutlineDisclosureOptions(style: .header)
cell.accessories = [.outlineDisclosure(options: disclosureOptions)]
let background = UIBackgroundConfiguration.clear()
cell.backgroundConfiguration = background
}
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, OutlineItem> { cell, _, menuItem in
var content = UIListContentConfiguration.cell()
content.text = menuItem.title
cell.contentConfiguration = content
let background = UIBackgroundConfiguration.clear()
cell.backgroundConfiguration = background
cell.accessories = self.splitViewWantsToShowDetail() ? [] : [.disclosureIndicator()]
}
dataSource = UICollectionViewDiffableDataSource<Section, OutlineItem>(collectionView: outlineCollectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, item: OutlineItem) -> UICollectionViewCell? in
// Return the cell.
if item.subitems.isEmpty {
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
} else {
return collectionView.dequeueConfiguredReusableCell(using: containerCellRegistration, for: indexPath, item: item)
}
}
// Load our initial data.
let snapshot = initialSnapshot()
dataSource.apply(snapshot, to: .main, animatingDifferences: false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment