Table Views

How to properly create complex tableviews

Why this is important

Tableviews are at the heart of iOS and we see them everywhere. Sometimes it is hard to create a tableview with multiple sections and different cells within each section. For example, your multi-section code might look something like this:

    // MARK: - TABLE VIEW
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell...
            return cell
        case 1:
            let cell = tableView.dequeueReusableCell...
            return cell
        case 2:
            let cell = tableView.dequeueReusableCell...
            return cell
        default:
            return UITableViewCell()
        }
    }

What is wrong with this? We are switching on Int, which has a lot more than 3 cases. To combat this, we use the default case to silence the error.

If we want to add a new section, or move around existing sections, we have go in and rearrange/add new cases to each function where we switch on the indexPath.section. This is also not very readable. If a new member/someone outside tried reading this, it would be very confusing.

Let's make this better

Let's see how we can fix this issue and make it more readable.

For this example I will be using an example from Ithaca Transit.

We can start by creating a new enum type:

Let us break this down. We have 3 new eum types.

ItemType

This enum defines the type of items within each section. For Ithaca Transit, search results contain both Bus Stops and Place Results (Google Places) mixed within one section. Creating this enum will allow having one section with multiple cell types.

SectionType

This defines the type of sections. With this example, we will have 3 sections. RecentSearches, favorites, and seeAllStops. This is pretty simple.

Section

Finally, we have an enum type that combines both ItemType and SectionType. It defines which type of section it is, and what items go in that section.

Implementation

Now let's use this in an example.

Woohoo, now as you can see this is much simpler. We have defined sections and this makes it a lot more readable. You can even have a helper function that takes in an itemType and returns a cell to clean up this code even more, but I'll leave that to you.

If you add a new section, you'll get errors telling you you are missing a case statement, which will make sure you keep your code safe and free of bugs.

Last updated