一个部分可能包含 1 个页眉、许多内容项和 1 个页脚。
对于 DiffableDataSource
,网上的大部分例子,都是用的enum
代表科。例如
func applySnapshot(_ animatingDifferences: Bool) {
var snapshot = Snapshot()
snapshot.appendSections([.MainAsEnum])
snapshot.appendItems(filteredTabInfos, toSection: .MainAsEnum)
dataSource?.apply(snapshot, animatingDifferences: animatingDifferences)
}
但是,当 Section 有动态内容页脚时,我们可能需要使用 struct 来表示 Section。例如import Foundation
struct TabInfoSection {
// Do not include content items [TabInfo] as member of Section. If not, any mutable
// operation performed on content items, will misguide Diff framework to throw
// away entire current Section, and replace it with new Section. This causes
// flickering effect.
var footer: String
}
extension TabInfoSection: Hashable {
}
但是,我们如何假设只更新页脚?目前提供的方法
DiffableDataSource: Snapshot Doesn't reload Headers & footers不完全准确
如果我尝试更新页脚
class TabInfoSettingsController: UIViewController {
…
func applySnapshot(_ animatingDifferences: Bool) {
var snapshot = Snapshot()
let section = tabInfoSection;
snapshot.appendSections([section])
snapshot.appendItems(filteredTabInfos, toSection: section)
dataSource?.apply(snapshot, animatingDifferences: animatingDifferences)
}
var footerValue = 100
extension TabInfoSettingsController: TabInfoSettingsItemCellDelegate {
func crossButtonClick(_ sender: UIButton) {
let hitPoint = (sender as AnyObject).convert(CGPoint.zero, to: collectionView)
if let indexPath = collectionView.indexPathForItem(at: hitPoint) {
// use indexPath to get needed data
footerValue = footerValue + 1
tabInfoSection.footer = String(footerValue)
//
// Perform UI updating.
//
applySnapshot(true)
}
}
}
我会得到以下闪烁的结果。闪烁的原因是diff框架抛出整个旧Section,并用新Section替换它,因为它发现
TabInfoSection
有变化。目的。有没有好办法,通过
DiffableDataSource
更新 Section 中的页脚不会造成闪烁效果?p/s 整个项目源代码可以在https://github.com/yccheok/ios-tutorial/tree/broken-demo-for-footer-updating中找到在 TabDemo 文件夹下。
最佳答案
你有没有想过只为页脚制作一个部分?这样一来,当它闪烁时就不会重新加载,因为它在技术上不属于有问题的部分?
关于ios - 如何通过DiffableDataSource更新Section中的页脚而不会造成闪烁效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64326881/