Outsource your UITableViewDataSource!

It is very common that the controller of an UITableView is also its data source. This is the number one reason why view controllers tend to become very massive. Outsourcing the data source to an object on its own is a better solution.

Hint: This post has been updated to Swift 3 and Xcode 8.

Let’s take a look at an example:

The table view displays three cells containing movie titles.  Nevertheless you can see in this simple example that there is already a lot of code in the view controller. If you add all the other stuff a view controller needs to do, it will become massive quickly.

So there is a better solution. We create a class called DataSource and move all the data source methods to that class:

Now, TableViewController just needs to create an instance of this object and set it as the table view’s data source:

The view controller is cleaned up now!

More generally, this method works not only with table views but with all views that need a data source, like  UICollectionView  or a view you have written on your own.



Title Image: @ enzozo / shutterstock.com

Book Tip

Big Nerd Range Guide: iOS Programming: Excellent introduction to iOS development. Some programming experience is recommended.