When reloading a table view data with NSDiffableDataSourceSnapshot
, the table view will only reload sections that contain differences from the previous snapshot.
I have a table view that contains entries with specific dates. The entries are ordered by sections for the entry week/month/custom date range, based on what the user had chosen in the segment control.
When I have a week section and this week is the only week that exist for this month, the section will not update when the user chooses a different date range and the header will stay the same.
How can I get all section headers to be reloaded regardless of if there is a difference between this and the previous snapshot?
Here is the code for setting up the snapshot:
struct EntriesSection {
let date: Date
var entries: [Entry] = []
}
var sections: [EntriesSection]()
private func setupSnapshot() {
snapshot = NSDiffableDataSourceSnapshot<Date, Entry>()
sections.forEach {
snapshot.appendSections([$0.date])
snapshot.appendItems($0.entries, toSection: $0.date)
}
dataSource?.apply(snapshot, animatingDifferences: true)
}
Results:
Thanks in advance.
The issue was with the title of the first section which was not changing.
The section is of type Date
- NSDiffableDataSourceSnapshot<Date, Entry>()
and both the week and the month start date was on the same day which is Dec 1, 2019.
So as far as the snapshot, the date is still the same, for both week and month.
In order to resolve it, I created an object which is a date range:
struct DateRange: Hashable {
let from: Date
let to: Date
}
And then changed the snapshot section type from Date
to DateRange
:
NSDiffableDataSourceSnapshot<DateRange, Entry>()
Now, if the start date is the same and the end date changes, the snapshot will know that he needs to update the header.