一个部分可能包含 1 个页眉、许多内容项和 1 个页脚。
对于DiffableDataSource
,大部分网上的例子,都是用enum
来表示Section。例如
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:快照不重新加载页眉和页脚并不完全准确
如果我尝试更新页脚
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 下找到。