I'm using a UITableViewDiffableDataSource with my UITableView and it works as expected. However I'm having trouble updating an item in the snapshot. I have a simple sample where;
enum Section {
case one
}
struct Item: Hashable {
let identifier: Int
let text: String
public func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
public static func == (lhs: Item, rhs: Item) -> Bool {
return lhs.identifier == rhs.identifier
}
}
with the initial snapshot created using
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.one])
snapshot.appendItems([
Item(identifier: 1, text: "One"),
Item(identifier: 2, text: "Two"),
Item(identifier: 3, text: "Three")
])
dataSource.apply(snapshot)
If I were to attempt updating an item using
var snapshot = dataSource.snapshot()
snapshot.reloadItems([
Item(identifier: 2, text: "Foo")
])
dataSource.apply(snapshot)
nothing happens. My expected result is for the text
of item with identifier
= 2 to be updated to "Foo". Note I have not included the table view cell provider code here.
I have read similar posts such as How to update a table cell using diffable UITableView and Can UITableViewDiffableDataSource detect an item changed? but the answer isn't clearly obvious.