Multiple UITableViewCells in UITableView

Learn how to add multiple UITableViewCells in a single UITableView with Swift.

Dejan Atanasov
Swift2Go
8 min readJun 25, 2017

--

This tutorial is focusing on how to add multiple UITableViewCells inside UITableView properly with the minimum code possible.

For this tutorial, I have picked the scenario where you have an app that contains screens like Contact Us, Register, Login etc. Simply said, you need to show forms on multiple screens with multiple types of cells. Of course, you can apply it to any other scenario where you need multiple UITableViewCells in UITableView.

Before we start there are a couple of things you show know:

Creating the UITableViewCells

First, we will start with the UITableViewCells creation. If you open the project you will see various cells created under the folder Cells which contain input fields, dropdowns, action buttons, multi-line input fields. For presentation purposes, I will use BaseCell and InputCell.

We will start by creating one master cell (named BaseCell) which will handle all the job for the children. BaseCell is used to store functions that are mutual in all its children. Then we will use those function as overrides in the child cells. This cell won’t have a UI but will take the suitable one from its children.

BaseCell

As you can see, I am creating one main function called set() which contains parameters that I need in order to “feed” all the children cells with data. Also, you can see another function called setForm() which is an example of a helper function if you don’t need to call some of the parameters. I also create helper functions for each parameter and then override them in the cell that requires that type of data. CellType is presented in the next section below.

Now, when we have the master cell in place we can start creating the children. As I have mentioned above, I will only present one child cell to keep things short, and you can follow the same flow for creating other child cells that you need. I still strongly recommend downloading the example project from Github.

InputCell

The InputCell is a cell that shows us a UITextField and a UILabel above it which acts as a cell title. As you can see, it inherits from the BaseCell and we override only the helper methods that we need inside the InputCell class. So, we need to populate this cell with a title, a placeholder for the UITextField, and also tracking the change of the input by adding a closure in the BaseCell called textChangedBlock?().

That’s it from the cell creation, now we continue with creating the enum types.

Enum Types

The enum types are here to help us organize the code properly and I highly suggest that you use enumerations in any situation possible. In our case, you can imagine them as Settings for our cells. I will need 2 enum types for this tutorial:

  1. CellType — all the UI settings for the child cell
  2. FormCellType — all the settings needed for populating the cells with data

CellType

As mentioned above, we will use the CellType for deciding the UI. Currently, you can see the getHeight() function which will return the height of each cell type, and also getClass() which returns the type of the cell for the given case.

FormCellType

FormCellType is the enum type where you need to define the fields that you are going to be using in the UITableView and its settings like title, placeholder, keyboardType etc. Also, pay attention of the cellType() function where we decide the CellType of the case.

UIViewController Flow

Let’s see how to combine everything that we have learned so far. I will start by creating a master controller. The purpose of creating a master controller is to save you from writing repetitive code, easy code reuse, and keeping your main classes clean and organized. In our case, all of the controllers that need multiple cells with inherit from BaseController. Here, we will store the UITableViewCellDelegate and UITableViewCellDataSource methods. Let me demonstrate what is going on here…

Starting from the top, cellTypes is an array that stores FormCellType enum values. We will declare in the master controller but will populate it in our child view controllers. You see how easily the height of the cell is passed to the heightForRowAt() delegate method. Same goes for the cellForRowAt() data source method, where you will just need to initialize the BaseCell and provide the reuseIdentifier from the child cell. The currentCell() function will be overridden whenever you need your cellForRowAt() data source method in your children controllers. That’s it for setting up the data source and delegate methods.

Now, all you need to do is just populate the cellTypes array in the child controllers with the cells you need, and it will fill in the UITableView instantly. For this tutorial, I have used only 1 child controller with 2 different forms, but you can easily use x number of child controllers and only fill in the cellTypes array. Look how short our controller would be…

ViewController

There you have it. You got a nicely organized controller with multiple UITableViewCells support in a single UITableView. 🙂
In order to register the UITableViewCells to the UITableView, I am using 2 extensions…

UITableView+Extensions.swift

UITableViewCell+Extensions.swift

That’s it from this tutorial and if it helped you please 👏 or share this story so others like you can find it. Thank you for your attention! 🚀

Check out my latest projects:

Read more of my writing on Medium:

Subscribe to my Newsletter:

--

--

Dejan Atanasov
Swift2Go

Senior iOS & Android Developer | Creator of Swift2Go | @hackernoon writer | Hire Me: http://bit.ly/2XfxbBR