gpt4 book ai didi

ios - DiffableDataSource:快照不会重新加载页眉和页脚

转载 作者:行者123 更新时间:2023-12-01 17:50:00 25 4
gpt4 key购买 nike

我使用 UICollectionViewDiffableDataSourceUICollectionView 来显示多个部分的内容。

我正在使用 Collection View 组合布局和 Diffable 数据源 link它是在 WWDC'19 上引入的,用于渲染 UICollectionView

的多部分布局

我有一个简单的设置,每个部分的页眉显示该部分中的项目数,页脚显示该部分所有项目的摘要。

section 1 Header --> January 2020 - 5 Trips
section 1 item 1 --> Trip 1
section 1 item 2 --> Trip 2
section 1 item 3 --> Trip 3
section 1 item 4 --> Trip 4
section 1 item 5 --> Trip 5

现在,如果删除行程,DiffableDataSource 将通过动画更新更改,但不会重新加载各部分的标题。这看起来不一致。例如。如果行程 4 被删除,则标题仍显示该部分中有 5 个行程。如何使用 DiffableDataSource 重新加载 header ?

为了临时修复,我只是打电话collectionView.reloadData() 经过一段延迟后,显示了 Diffing 动画,然后我硬重新加载数据,这也强制重新加载 header 。

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)
}

最佳答案

要触发 header 的自动重新加载,您的Section对象应该是可哈希的,并且应该存储所有必要的属性来创建唯一的哈希对于该部分。

这就是为什么所有 Section 对象和 Item 对象都应该是 Hashable 并且它们应该返回一个唯一的哈希值以允许DiffableDataSource 仅当其值发生更改时才管理其重新加载。

例如:

struct MonthSection: Hashable {
var title: String
var itemsCount: Int
}

然后:

var section = MonthSection(title: "Title", itemsCount: 5)
.....
snapshot.appendSections([section])
snapshot.appendItems(items, toSection: section)

在下次快照更新期间,该部分的标题或项目计数的任何更改都将触发部分标题重新加载,这将像魔术一样工作!

关于ios - DiffableDataSource:快照不会重新加载页眉和页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59980398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com