We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
func configureDataSource() { let cellRegistration = UICollectionView.CellRegistration <LabelCell, MountainsController.Mountain> { (cell, indexPath, mountain) in // Populate the cell with our item description. cell.label.text = mountain.name } dataSource = UICollectionViewDiffableDataSource<Section, MountainsController.Mountain>(collectionView: mountainsCollectionView) { (collectionView: UICollectionView, indexPath: IndexPath, identifier: MountainsController.Mountain) -> UICollectionViewCell? in // Return the cell. return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: identifier) } }
UICollectionViewDiffableDataSource
func performQuery(with filter: String?) { let mountains = mountainsController.filteredMountains(with: filter).sorted { $0.name < $1.name } var snapshot = NSDiffableDataSourceSnapshot<Section, MountainsController.Mountain>() snapshot.appendSections([.main]) snapshot.appendItems(mountains) dataSource.apply(snapshot, animatingDifferences: true) }
class MountainsController { struct Mountain: Hashable { let name: String let height: Int let identifier = UUID() func hash(into hasher: inout Hasher) { hasher.combine(identifier) } static func == (lhs: Mountain, rhs: Mountain) -> Bool { return lhs.identifier == rhs.identifier } func contains(_ filter: String?) -> Bool { guard let filterText = filter else { return true } if filterText.isEmpty { return true } let lowercasedFilter = filterText.lowercased() return name.lowercased().contains(lowercasedFilter) } } func filteredMountains(with filter: String?=nil, limit: Int?=nil) -> [Mountain] { let filtered = mountains.filter { $0.contains(filter) } if let limit = limit { return Array(filtered.prefix(through: limit)) } else { return filtered } } private lazy var mountains: [Mountain] = { return generateMountains() }() } extension MountainsController { private func generateMountains() -> [Mountain] { let components = mountainsRawData.components(separatedBy: CharacterSet.newlines) var mountains = [Mountain]() for line in components { let mountainComponents = line.components(separatedBy: ",") let name = mountainComponents[0] let height = Int(mountainComponents[1]) mountains.append(Mountain(name: name, height: height!)) } return mountains } }
func configureDataSource() { wifiController = WiFiController { [weak self] (controller: WiFiController) in guard let self = self else { return } self.updateUI() } self.dataSource = UITableViewDiffableDataSource <Section, Item>(tableView: tableView) { [weak self] (tableView: UITableView, indexPath: IndexPath, item: Item) -> UITableViewCell? in guard let self = self, let wifiController = self.wifiController else { return nil } let cell = tableView.dequeueReusableCell( withIdentifier: WiFiSettingsViewController.reuseIdentifier, for: indexPath) var content = cell.defaultContentConfiguration() // network cell if item.isNetwork { content.text = item.title cell.accessoryType = .detailDisclosureButton cell.accessoryView = nil // configuration cells } else if item.isConfig { content.text = item.title if item.type == .wifiEnabled { let enableWifiSwitch = UISwitch() enableWifiSwitch.isOn = wifiController.wifiEnabled enableWifiSwitch.addTarget(self, action: #selector(self.toggleWifi(_:)), for: .touchUpInside) cell.accessoryView = enableWifiSwitch } else { cell.accessoryView = nil cell.accessoryType = .detailDisclosureButton } } else { fatalError("Unknown item type!") } cell.contentConfiguration = content return cell } self.dataSource.defaultRowAnimation = .fade wifiController.scanForNetworks = true }
The text was updated successfully, but these errors were encountered:
WWDC Advances in UI Data Sources
UITableViewDiffableDataSource
NSCollectionViewDiffableDataSource
NSDiffableDataSourceSnapshot
SectionIdentifierType
ItemIdentifierType
Hashable
Sorry, something went wrong.
No branches or pull requests
MountainsView
UICollectionViewDiffableDataSource
에WiFiSettingsView
The text was updated successfully, but these errors were encountered: