0

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

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())
4

1 回答 1

0

事实证明,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
    }
}
于 2021-03-24T19:36:54.697 回答