【问题标题】:UICollectionViewDiffableDataSource doesn't animate properly on applyUICollectionViewDiffableDataSource 在应用时没有正确动画
【发布时间】:2021-06-21 13:59:38
【问题描述】:

我有一个项目列表,它们在列表中按数量降序显示。我按名称对它们进行哈希处理,因此当我更改数量时,它们应该在应用时顺利重新排序。 当我在UICollectionViewDiffableDataSource 上调用apply(snapshot, animatingDifferences: true) 函数时,它不会以流畅的动画对单元格重新排序,而是会闪烁,并且一切都已就位,没有“重新排序”。

struct Item: Hashable {
    let name: String
    let amount: Int

    func hash(into hasher: inout Hasher) {
        hasher.combine(name)
    }
}

// when user taps, I call this
dataSource.apply(makeSnapshot())

【问题讨论】:

  • 你的UICollectionViewDiffableDataSource<SectionType, ItemType>是什么类型的?

标签: ios swift uicollectionview uicollectionviewcompositionallayout uicollectionviewdiffabledatasource


【解决方案1】:

原来UICollectionViewDiffableDataSource 不仅使用哈希来判断哪个项目是哪个项目,还使用== 运算符。 这解决了问题:

struct Item: Hashable {
    let name: String
    let amount: Int

    func hash(into hasher: inout Hasher) {
        hasher.combine(name)
    }

    static func == (lhs: Category, rhs: Category) -> Bool {
        return lhs.name == rhs.name
    }
}

【讨论】:

  • 有趣,当时的文档中是否引用了有关 == 的详细信息?
猜你喜欢
  • 2023-03-21
  • 2012-06-21
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 2021-07-22
  • 1970-01-01
相关资源
最近更新 更多