ios - 如何通过DiffableDataSource更新Section中的页脚而不会造成闪烁效果?

标签 ios swift diffabledatasource

一个部分可能包含 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)
        }
    }
}
我会得到以下闪烁的结果。
enter image description here
闪烁的原因是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/

相关文章:

iphone - 登录时的按钮操作 FBConnect API 中的按钮单击

ios - 从 UIMenuController 中删除复制、查找和共享

ios - 从推送通知打开应用程序时打开特定的 View Controller

ios - 使用 UITableViewDiffableDataSource 创建章节标题

ios - 在初始化所有成员之前, self 被闭包捕获 - 但我确实初始化了它们

ios - 如何创建模型类以及如何与 TableView 中的部分一起使用

iOS应用程序分发

objective-c - 在 Textfield 中,在 ios 5 中完美工作时,无法使用 ios 6 和 xcode 4.5 键入任何内容

ios - 启用禁用的选项卡

Swift DiffableDataSource 进行插入和删除而不是重新加载