高级 UICollectionView & UITableView

一个为 UICollectionViewUITableView 提供的框架,提供:

UICollectionViewDiffableDataSource & TableViewDiffableDataSource

处理程序

提供用于选择、重新排序、聚焦和编辑单元格的处理程序。

一些包含的处理程序示例

// Handler that gets called when the user did select an item.
diffableDataSource.selectionHandlers.didSelect = { itemIdentifier in

}

// Handler that gets called when an item/cell is about to be shown.
diffableDataSource.displayingHandlers.willDisplay = { itemIdentifier, cell in

}

TableViewDiffableDataSource 提供了用于重新排序单元格的处理程序,类似于 Apple 提供的 UICollectionViewDiffableDataSource 重新排序处理程序

// Allow every item to be reordered
tableViewDataSource.reorderingHandlers.canReorder = { item in return true }

// Update the backing store from the did reorder transaction.
tableViewDataSource.reorderingHandlers.didReorder = { [weak self] transaction, _ in
    guard let self = self else { return }
             
    if let updatedCurrentItems = self.currentItems.applying(transaction.difference) {
        self.currentItems = updatedCurrentItems
    }
}

空数据源视图

当数据源不包含任何项目时,emptyView 显示提供的视图

diffableDataSource.emptyView = myEmptyView

或者,您可以使用 emptyContentConfiguration 并提供一个 UIContentConfiguration

let loadingConfiguration = UIContentUnavailableConfiguration.loading()
diffableDataSource.emptyContentConfiguration = loadingConfiguration

UITableView 单元格注册

类似于 UICollectionView.CellRegistration 的表格视图单元格注册。

使用单元格注册来向表格视图注册表格单元格视图,并配置每个单元格以进行显示。

以下示例为 UITableViewCell 类型的单元格和字符串项目创建一个单元格注册。 每个单元格的文本字段显示其项目字符串。

let cellRegistration = UITableView.CellRegistration<UITableViewCell, String> { cell, indexPath, string in
    var contentConfiguration = cell.defaultContentConfiguration()

    contentConfiguration.text = string
    contentConfiguration.textProperties.color = .lightGray

    cell.contentConfiguration = contentConfiguration
}

创建单元格注册后,将其传递到 dequeueConfiguredReusableCell(using:for:item:),该方法从数据源的单元格提供程序调用。

dataSource = UITableViewDiffableDataSource<Section, String>(tableView: tableView) {
    tableView, indexPath, item in
    return tableView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
}

或者,您可以使用 UICollectionViewDiffableDataSourceUITableViewDiffableDataSource 初始化器

let dataSource = UITableViewDiffableDataSource(tableView: myTableView, cellRegistration: cellRegistration)
}

UITableView 区头视图注册

表格视图的页眉/页脚区头视图的注册。

以下示例为具有字符串的分区的 UITableViewHeaderFooterView 类型的视图创建一个区头视图注册。 每个区头视图的默认内容配置文本显示其分区字符串。

let sectionViewRegistration = UITableView.SectionViewRegistration<UITableViewHeaderFooterView, String> {
    sectionView, indexPath, string in
     
    var configuration = sectionView.defaultContentConfiguration()
    configuration.text = string
    sectionView.contentConfiguration = configuration
}

创建区头视图注册后,将其传递到 dequeueConfiguredReusableSectionView(using:section:),该方法从数据源的区头视图提供程序调用。

dataSource.headerViewProvider = { tableView, section in
    return tableView.dequeueConfiguredReusableSectionView(using: sectionViewRegistration, section: section)
}

或者,您也可以使用 applyHeaderViewRegistration()applyFooterViewRegistration()

dataSource.applyHeaderViewRegistration(sectionViewRegistration)

可差异化数据源快照应用选项

用于将快照应用于可差异化数据源的选项

// Applies the snapshot animated with default animation duration.
dataSource.apply(mySnapshot, .animated)

// Applies the snapshot animated with the specified animation duration.
dataSource.apply(mySnapshot, .animated(duration: 2.0))

// Applies the snapshot non animated.
dataSource.apply(mySnapshot, .withoutAnimation)

// Applies the snapshot using reload data.
dataSource.apply(mySnapshot, .usingReloadData)