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

Nearby Home

Enum based datasource implementation

Preparing the data source:

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

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.


  • 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.

Protocols to the rescue

Didn’t get it? Wait for the code.

Presentation Models

Presentation Models

Abstracting Cells

Cell abstraction

View Controller Implementation

Isn’t that great…!!


I would love to hear from you




Software Developer(iOS), Speaker & Writer at Swift India

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Comprehensive Introductory Guide to Supervised Learning for the Non-Mathematician

The ‘Colaboratory’ Data Scientist: Working in the cloud.

Threat Modeling simple questions

Thoughts on My First Hackathon

The Art of Web Scraping

Image of person at keyboard

A Simple Approach to Data — Analysis of NYC Airbnb Listings

Get Started Analyzing your CGM data in 5 Easy Steps

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
G. Abhisek

G. Abhisek

Software Developer(iOS), Speaker & Writer at Swift India

More from Medium

Generics and InOut Parameters in Swift with Examples

Stacks in Swift — Data Structure

API for Dummies

100 Days of SwiftUI Learning — Day 4