Predefined cell accessories for UICollectionViewListCell.
Last active
March 13, 2024 22:31
-
-
Save AngrenWen/f9c412fd318f55b3927948c036a77960 to your computer and use it in GitHub Desktop.
Presentation of predefined UICollectionView cells (iOS 14)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class CellAttributesShowcaseListViewController: UICollectionViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
collectionView.collectionViewLayout = listLayout | |
applyInitialSnapshot() | |
} | |
private var listLayout: UICollectionViewCompositionalLayout { | |
let listConfiguration = UICollectionLayoutListConfiguration(appearance: .grouped) // .plain, .grouped, .insetGrouped, .sidebar, .sidebarPlain | |
return UICollectionViewCompositionalLayout.list(using: listConfiguration) | |
} | |
private var cellRegistration: UICollectionView.CellRegistration<UICollectionViewListCell, String> { | |
UICollectionView.CellRegistration { (cell: UICollectionViewListCell, indexPath: IndexPath, itemIdentifier: String) in | |
var contentConfiguration = cell.defaultContentConfiguration() | |
contentConfiguration.secondaryText = "collectionView.dataSource." | |
contentConfiguration.text = itemIdentifier | |
switch indexPath.row { | |
case 0: | |
cell.accessories = [.disclosureIndicator(displayed: .always)] | |
case 1: | |
cell.accessories = [.outlineDisclosure()] | |
case 2: | |
cell.accessories = [.checkmark()] | |
case 3: | |
cell.accessories = [.delete(displayed: .whenNotEditing)] | |
case 4: | |
cell.accessories = [.insert(displayed: .always)] | |
case 5: | |
cell.accessories = [.reorder(displayed: .always)] | |
case 6: | |
cell.accessories = [.multiselect(displayed: .always)] | |
case 7: | |
cell.accessories = [.label(text: "label")] | |
default: | |
cell.accessories = [.outlineDisclosure()] | |
break | |
} | |
cell.contentConfiguration = contentConfiguration | |
} | |
} | |
private func makeDataSource() -> UICollectionViewDiffableDataSource<Int, String> { | |
return UICollectionViewDiffableDataSource<Int, String>(collectionView: collectionView) { (collectionView: UICollectionView, indexPath: IndexPath, itemIdentifier: String) in | |
collectionView.dequeueConfiguredReusableCell(using: self.cellRegistration, for: indexPath, item: itemIdentifier) | |
} | |
} | |
private func applyInitialSnapshot() { | |
var snapshot = NSDiffableDataSourceSnapshot<Int, String>() | |
snapshot.appendSections([0]) | |
snapshot.appendItems(CellData.sampleData.map { $0.title }) | |
let dataSource = makeDataSource() | |
dataSource.apply(snapshot) | |
collectionView.dataSource = dataSource | |
} | |
} | |
struct CellData { | |
let title: String | |
let subtitle: String? | |
} | |
#if DEBUG | |
extension CellData { | |
static let sampleData = [ | |
CellData(title: ".disclosureIndicator", subtitle: "a disclosure indicator system accessory with the specified display state and configuration options"), | |
CellData(title: ".outlineDisclosure", subtitle: "an outline disclosure system accessory with the specified display state, configuration options, and optional action handler"), | |
CellData(title: ".checkmark", subtitle: "a checkmark system accessory with the specified display state and configuration options"), | |
CellData(title: ".delete", subtitle: "a delete system accessory with the specified display state, configuration options, and optional action handler"), | |
CellData(title: ".insert", subtitle: "an insert system accessory with the specified display state, configuration options, and optional action handler"), | |
CellData(title: ".reorder", subtitle: "a reorder system accessory with the specified display state and configuration options"), | |
CellData(title: ".multiselect", subtitle: "a multiselect system accessory with the specified display state and configuration options"), | |
CellData(title: ".label", subtitle: "a label system accessory with the specified text, display state, and configuration options"), | |
CellData(title: ".customView", subtitle: "a custom view accessory"), | |
] | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment