Two UITableViews One UIViewController

That must be chocolate ice-creme

Photo by Tim Graf on Unsplash



UITableView: A view that presents data using rows arranged in a single column

The challenge

What would happen if I were to whack two UITableView instances into a single view controller?

Setting up the storyboard

You could also attempt this programatically (or however you want to, I don’t even know you so go wild).

The code

The outlets and data

Within the brilliantly named UIViewController ViewController we set up a couple of outlets for the UITableView instances, and prepare some (creative) data for them to display.

Click for Gist

The viewDidLoad function

This is then followed up in the viewDidLoad() function by setting up the cell for reuse.

Click for Gist

The UITableViewDataSource

The minimal implementation of UITableViewDataSource implements tableView(_:numberOfRowsInSection:) and tableView(_:cellForRowAt:). The question is which tableView is the function called from? It turns out that this is called by both, however one of the function parameters is actually the tableview.

Click for Gist
Click for Gist


Creating a table view is really important when creating Apps using the iOS SDK.

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