Elegantly prepare data source for Table View and Collection View

Table view data sourced methods
Great..!! You are friends with the Justice League
Single cell-type typical implementation.

Multiple cell types

Let us take the example of the below screen. This screen is from the Nearby app from my blogpost on MVVM. The Nearby app fetches JSON from Google APIs.

Nearby Home
Nearby.json

Enum based datasource implementation

Preparing the data source:

Though there are a lot of ways to configure the datasource for the above cell, one would generally prefer to configure datasource based on the cell type as it would help to determine which cell to display and what data to show. Or else you can go for a lot of if-else checks.

Enum based modeling and datasource configuration
  • TopPicksCell can be populated with a list of Place thereby the associated value is an array of Place
  • CategoriesCell is a cell with a collection view that can be populated using a list of categories.
  • We have multiple places cells grouped under a common category type. A PlacesCell can be build using a list of Place .

Populating the data

A typical implementation of this approach will be something like this:

Enum based datasource approach
  • We have declared a homeListDatasource property that holds all the cell types and their associated values. I believe the logic to prepare the homeListDatasource is pretty straightforward based on the details of the cell data that we discussed earlier.
  • In the table view’s datasource method, we distinguished between the cell with the help of our HomeListCellType enums.
  • We created methods to provide cells based on the cell data that we got from our associated value.

Problem

  • We have switch cases spread everywhere.
  • Our ViewController will bloat with logic to configure the cell.
  • Repetitive cell configuration methods in ViewController.
  • If cell types increase, you can imagine how ugly our ViewController would look.
Why?

Protocols to the rescue

If you look closely, you would find many repetitive patterns in our implementation. The creation of cells is duplicated and even the configuration calls for the cells might seem duplicated as well except the data that we are passing to each cell.

Didn’t get it? Wait for the code.

Presentation Models

Let us abstract all details needed by the cells in Presentation models which would confirm a common protocol.

Presentation Models

Abstracting Cells

Cells can also be abstracted in a similar manner.

Cell abstraction

View Controller Implementation

Now we can have a list of TableCellRepresentable as our datasource.

Isn’t that great…!!

Problem

Typecasting and type safety might be a problem that comes as a trade-off for making your code short. For logging any abuse of the protocols, we can log the errors if any as done in the above implementation.

I would love to hear from you

You can reach me for any query, feedback or just want to have a discussion by the following channels:

--

--

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