Swift NSDiffableDataSourceSectionSnapshot 重新加载部分
Swift NSDiffableDataSourceSectionSnapshot Reload Section
我目前正在学习 DiffableDataSource。我正在使用 NSDiffableDataSourceSectionSnapshot(),而不是 NSDiffableDataSourceSnapshot()
为我的数据源
创建部分
根据点击的部分,我想增加部分模型数据counter
属性。但是,我在使用 sectionSnapshot()
重新加载该部分时遇到问题。总counter
属性 后面会显示为sectionHeader.
下面是我的结构
struct TestParent: Hashable {
var title = String()
var counter = 0
var children = [TestChildren]()
}
struct TestChildren: Hashable {
var title = String()
var name = String()
}
下面是我的实现代码
@IBOutlet weak var collectionView: UICollectionView!
private var dataSource: UICollectionViewDiffableDataSource<AnyHashable, AnyHashable>?
private var parents = [TestParent]()
override func viewDidLoad() {
super.viewDidLoad()
self.createSampleArray()
self.configureAllCollectionViews()
}
fileprivate func createSampleArray() {
self.parents = [
TestParent(title: "Parent 1",
children: [
TestChildren(title: "Child 1 - A"),
TestChildren(title: "Child 1 - B"),
TestChildren(title: "Child 1 - C"),
]),
TestParent(title: "Parent 2",
children: [
TestChildren(title: "Child 2 - A"),
TestChildren(title: "Child 2 - B"),
TestChildren(title: "Child 2 - C"),
TestChildren(title: "Child 2 - D"),
]),
]
}
private func parentSnapshot(_ parent: TestParent) -> NSDiffableDataSourceSectionSnapshot<AnyHashable> {
var snapshot = NSDiffableDataSourceSectionSnapshot<AnyHashable>()
snapshot.append(parent.children)
return snapshot
}
private func applyInitialSnapshot() {
for eachParent in parents {
self.dataSource?.apply(self.parentSnapshot(eachParent), to: eachParent)
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.check(indexPath: indexPath)
}
fileprivate func check(indexPath: IndexPath) {
guard let children = self.dataSource?.itemIdentifier(for: indexPath) as? TestChildren else { return }
guard var section = self.dataSource?.snapshot().sectionIdentifier(containingItem: children) as? TestParent else { return }
section.counter += 1
var snapshot = self.dataSource?.snapshot()
snapshot?.reloadSections([section])
self.dataSource?.apply(snapshot!)
}
如何在每次单击单元格时更新部分数据模型 counter
属性?构建collectionView没有问题UI
我在这里错过了什么?谢谢...
你的部分是一个结构——一种值类型——所以当你改变部分计数时,你改变了 diffable 数据源用作标识符的散列。它不知道您正在更新一个预先存在的部分;它认为这是一个全新的部分。
一般来说,部分标识符应该是标识符并且不包含可变状态。您可以编写散列和相等方法来忽略该可变状态,但考虑它是一件痛苦的事情,所以只需将实际数据保留在其他地方并根据部分标识符进行查找。
我目前正在学习 DiffableDataSource。我正在使用 NSDiffableDataSourceSectionSnapshot(),而不是 NSDiffableDataSourceSnapshot()
为我的数据源
根据点击的部分,我想增加部分模型数据counter
属性。但是,我在使用 sectionSnapshot()
重新加载该部分时遇到问题。总counter
属性 后面会显示为sectionHeader.
下面是我的结构
struct TestParent: Hashable {
var title = String()
var counter = 0
var children = [TestChildren]()
}
struct TestChildren: Hashable {
var title = String()
var name = String()
}
下面是我的实现代码
@IBOutlet weak var collectionView: UICollectionView!
private var dataSource: UICollectionViewDiffableDataSource<AnyHashable, AnyHashable>?
private var parents = [TestParent]()
override func viewDidLoad() {
super.viewDidLoad()
self.createSampleArray()
self.configureAllCollectionViews()
}
fileprivate func createSampleArray() {
self.parents = [
TestParent(title: "Parent 1",
children: [
TestChildren(title: "Child 1 - A"),
TestChildren(title: "Child 1 - B"),
TestChildren(title: "Child 1 - C"),
]),
TestParent(title: "Parent 2",
children: [
TestChildren(title: "Child 2 - A"),
TestChildren(title: "Child 2 - B"),
TestChildren(title: "Child 2 - C"),
TestChildren(title: "Child 2 - D"),
]),
]
}
private func parentSnapshot(_ parent: TestParent) -> NSDiffableDataSourceSectionSnapshot<AnyHashable> {
var snapshot = NSDiffableDataSourceSectionSnapshot<AnyHashable>()
snapshot.append(parent.children)
return snapshot
}
private func applyInitialSnapshot() {
for eachParent in parents {
self.dataSource?.apply(self.parentSnapshot(eachParent), to: eachParent)
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.check(indexPath: indexPath)
}
fileprivate func check(indexPath: IndexPath) {
guard let children = self.dataSource?.itemIdentifier(for: indexPath) as? TestChildren else { return }
guard var section = self.dataSource?.snapshot().sectionIdentifier(containingItem: children) as? TestParent else { return }
section.counter += 1
var snapshot = self.dataSource?.snapshot()
snapshot?.reloadSections([section])
self.dataSource?.apply(snapshot!)
}
如何在每次单击单元格时更新部分数据模型 counter
属性?构建collectionView没有问题UI
我在这里错过了什么?谢谢...
你的部分是一个结构——一种值类型——所以当你改变部分计数时,你改变了 diffable 数据源用作标识符的散列。它不知道您正在更新一个预先存在的部分;它认为这是一个全新的部分。
一般来说,部分标识符应该是标识符并且不包含可变状态。您可以编写散列和相等方法来忽略该可变状态,但考虑它是一件痛苦的事情,所以只需将实际数据保留在其他地方并根据部分标识符进行查找。