对于UICollectionViewDiffableDataSource,我想知道,在不需要构建整个NSDiffableDataSourceSnapshot快照的情况下,可以直接对给定的部分/行执行单元格更新吗?
有时,我需要对选定的部分/选定的行执行实时的连续更新。必须为每一次实时更新构建整个数据源快照,似乎效率不高。
如果有可能的话,我想知道所涉及的步骤是甚麽?
如果这是不可能的,是不是因为UICollectionViewDiffableDataSource框架的设计决定?
发布于 2022-05-12 18:05:12
自iOS15以来,我们有一种有效的方法来更新选定的项行
private func reconfigureRecordingRow(_ recording: Recording) {
var snapshot = dataSource.snapshot()
snapshot.reconfigureItems([recording])
dataSource.apply(snapshot)
}
private func makeDataSource() -> DataSource {
let dataSource = DataSource(
collectionView: collectionView,
cellProvider: { [weak self] (collectionView, indexPath, anyHashable) -> UICollectionViewCell? in
guard let self = self else { return nil }
guard let recordingCell = collectionView.dequeueReusableCell(
withReuseIdentifier: "recording",
for: indexPath) as? RecordingCell else {
return nil
}当调用reconfigureRecordingRow时,cellProvider的函数将被执行。
collectionView.dequeueReusableCell能够重用现有的 UICollectionViewCell,而无需构建新的UICollectionViewCell。
https://stackoverflow.com/questions/72186812
复制相似问题