0

I have a list of items, they are displayed in decreasing order by amount in a list. I hash them by name, so when I change the amounts, they should reorder smoothly on apply. When I call the apply(snapshot, animatingDifferences: true) function on UICollectionViewDiffableDataSource, it doesn't reorder the cells with a smooth animation, but flashes instead and everything is in place with no "reordering".

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())
Bence Pattogato
  • 3,752
  • 22
  • 30

1 Answers1

0

Turns out the UICollectionViewDiffableDataSource not only uses the hash to tell which item is which, but the == operator too. This solved the problem:

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
    }
}
Bence Pattogato
  • 3,752
  • 22
  • 30