Two UITableViews One UIViewController

That must be chocolate ice-creme

Image for post
Image for post
Photo by Tim Graf on Unsplash

Prerequisites:

Terminology

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?

Image for post
Image for post

Setting up the storyboard

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

Image for post
Image for post

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.

Image for post
Image for post
Click for Gist

The viewDidLoad function

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

Image for post
Image for post
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.

Image for post
Image for post
Click for Gist
Image for post
Image for post
Click for Gist

Conclusion

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