UITableView Diffable DataSource Part 1

What was the problem?

Understanding how a diffable data source is defined

class UITableViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType> : NSObject 
where SectionIdentifierType : Hashable, ItemIdentifierType : Hashable
struct NSDiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType> 
where SectionIdentifierType : Hashable, ItemIdentifierType : Hashable

Getting Started

Figure 1
  1. Init NsDiffableDataSource. It's a generic type, like the UITableViewDiffableDataSource.
  2. Add the main section and the data in it.
  3. Apply the snapshot on dataSource.
  4. animatedDifferences as false, this parameter as false means that we don’t want the tableView getting animated in this snapshot.
    In fact, we don’t want to animate at the first iteration. We want the first elements already in the tableView, not seeing them getting added on it.
Figure 2
Figure 3
Figure 4
Figure 5
Figure 6
  1. For 1 section example we only conform to Hashable which means all value in the object should be same to generate same hash value and same for == operator
  2. For 2 section example we conform to Hashable and say has should be generate with serialNumber which means if onlyserialNumber value in object should be same to generate same hash value and for == operator still whole object value will same
  3. For last section if serialNumber in objects is same it will produce same hash and both object are equal as well
Figure 7
Figure 8
Figure 9
Figure 10

Declaration

@MainActor class UITableViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType> : NSObject where SectionIdentifierType : Hashable, ItemIdentifierType : Hashable

Discussion

  1. Connect a diffable data source to your table view.
  2. Implement a cell provider to configure your table view’s cells.
  3. Generate the current state of the data.
  4. Display the data in the UI.
Figure 11
Figure 12

Insert Section with animation

Gif1
Figure 13
Figure 14
Gif2
Gif3
Figure 15
Figure 16
Figure 17
Figure 18

Diffable DataSource Summary

Figure 19
Figure 20
Gif4

Directly Calling Diffable DataSource API’s

Insert Item

Delete Item

Figure 21
Figure 22

Update Item

Gif5
Figure 23
Figure 24
Figure 25

Useful Links

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ali Akhtar

Ali Akhtar

Senior iOS Engineer | HungerStation | Delivery Hero