【发布时间】:2020-05-15 17:40:38
【问题描述】:
我将UICollectionViewDiffableDataSource 用于UICollectionView 以在多个部分中显示内容。
我正在使用 WWDC'19 中引入的 Collection View Compositional Layout 和 Diffable Datasources link 来呈现 UICollectionView 的多部分布局
我有一个简单的设置,每个部分的页眉显示该部分中的项目数,页脚显示该部分所有项目的摘要。
第 1 节标题 --> 2020 年 1 月 - 5 次旅行
第 1 部分第 1 项 --> 行程 1
第 1 部分第 2 项 --> 行程 2
第 1 部分第 3 项 --> 行程 3
第 1 部分第 4 项 --> 行程 4
第 1 节第 5 项 --> 行程 5
现在如果删除行程,DiffableDataSource 会通过动画更新更改,但不会重新加载部分的标题。这看起来不一致。例如。如果行程 4 已删除,则标题仍显示该部分中有 5 个行程。如何让标头也使用 DiffableDataSource 重新加载?
为了临时修复,我只是打电话给
collectionView.reloadData() 在显示 Diffing 动画的延迟之后,然后我硬重新加载数据,这也会强制重新加载标题。
private func configureTripDataSource(){
tripDataSource = UICollectionViewDiffableDataSource<MonthSection, Trip>(collectionView: tripsCollectionView, cellProvider: { (collectionView, indexPath, trip) -> UICollectionViewCell? in
// Get a cell of the desired kind.
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: TripInfoCell.reuseIdentifier,
for: indexPath) as? TripInfoCell else { fatalError("Cannot create new TripInfoCell") }
// Populate the cell with our item description.
cell.trip = trip
// Return the cell.
return cell
})
tripDataSource.supplementaryViewProvider = {
[weak self] (collectionView: UICollectionView, kind: String, indexPath: IndexPath) -> UICollectionReusableView? in
guard let self = self else {return nil}
if kind == TripsController.tripsMonthSectionHeaderElementKind{
// Get a supplementary view of the desired kind.
guard let header = collectionView.dequeueReusableSupplementaryView(
ofKind: kind,
withReuseIdentifier: TripSectionHeaderCell.reuseIdentifier,
for: indexPath) as? TripSectionHeaderCell else { fatalError("Cannot create new header") }
// setup header
let currentSnapShot = self.tripDataSource.snapshot()
let tripMonthSection = currentSnapShot.sectionIdentifiers[indexPath.section]
header.titleLabel.text = tripMonthSection.title
header.subtitleLabel.text = "\(tripMonthSection.trips.count) Trips"
return header
} else {
return UICollectionReusableView()
}
}
var snapshot = NSDiffableDataSourceSnapshot<MonthSection, Trip>()
let allSections = self.tripsStore.monthSections
snapshot.appendSections(allSections)
for section in allSections{
snapshot.appendItems(section.trips, toSection: section)
}
self.tripDataSource.apply(snapshot, animatingDifferences: true)
}
【问题讨论】:
-
请添加页眉页脚相关代码
-
@vadian,好的,我已经添加了代码。
-
抱歉,我忽略了您使用的是我不熟悉的
supplementaryViewProvider。 -
我也有同样的问题。我试过取消标题并重新设置它,但没有运气。它仅在滚动离开屏幕并返回时才会更新。
标签: ios swift uicollectionview uicollectionviewlayout diffabledatasource