DEV Community

Igor Custodio
Igor Custodio

Posted on AI-assisted

Managing UITableView with Generics - Build & Tail #001

A small Swift helper for showing and hiding sections and working with different cell types.

Yes, we are in 2026, and everyone is talking about AI and the present and future of Engineering. However, I am here to share some good stuff I made earlier in my career.

The Context

A few years ago, at the company where I worked, we used the same structure for most of our table views. Each screen had its own sections and cells, but the code around them looked familiar every time.

We even had Xcode templates. They made it faster to create all the code we’d still have to maintain.

The Problem

Showing or hiding a section was pretty manual. If the first section disappeared, the second one became section 0. The code that counted sections and the code that looked up a cell both had to agree on what section 0 meant.

That was the kind of work we kept doing from screen to screen. I wanted to write it once.

The Idea

I created TableViewData to keep an ordered list of sections and give the table view a filtered list of the ones it should display. Each section kept its rows even while it was hidden. Showing it again meant changing a flag.

Generics let each screen define its own section and row types. A section could contain several kinds of rows, and the screen could get to each one through the same lookup.

The Implementation

TableViewData takes two types, one for sections and one for rows. Each section holds its own rows and a visible flag. The helper keeps the sections in the order you add them, and visibleSections gives the table view the ones it should display. When you hide a section, its rows are still there when you show it again.

When the table view asks for a cell, rowType(in:in:) looks up the row using its position on screen. The screen can then switch on the returned value to choose and configure the right cell. A section can mix text rows and action rows, and both go through the same lookup.

open class TableViewData<W: TableViewEnumType, U: TableViewEnumType> {
    public typealias DataSection = TableViewDataSection<W, U>

    public private(set) var sections: [DataSection] = []

    public var visibleSectionCount: Int {
        visibleSections.count
    }

    public var visibleSections: [DataSection] {
        sections.filter(\.visible)
    }

    public func sectionType(at section: Int) -> W {
        visibleSections[section].type
    }

    public func rowType(in section: Int, in row: Int) -> U {
        visibleSections[section].rows[row]
    }

    public func add(sections: DataSection...) {
        add(sections: sections)
    }
}

public protocol TableViewEnumType: EquatableAssociateValue {} // special protocol for enums
Enter fullscreen mode Exit fullscreen mode

View the full Swift code and the settings example on GitHub.

The Trade-offs

This setup adds a class to each table view to manage its content. For a screen with one section and one kind of cell, I can see why someone would keep that code in the data source. The extra class earns its place as the screen has more kinds of rows and sections that come and go.

There are also two meanings for section 0 in the full implementation. rowType(in:in:) looks at the first visible section, while remove(in:in:) looks at the first stored section. If the first stored section is hidden, the same index can point to two different sections. Passing the section itself to the removal method would make that choice explicit.

The Result

Say a screen has an optional details section. To hide it, the screen sets details.visible = false and reloads the table view. The section count and row lookups already use the filtered list. When the screen shows the details again, it gets the same rows back in the same place.

Adding another kind of cell works the same way. The screen adds a case to its row enum and handles it when configuring the cell. The section and row lookups stay the same.

The Tail

The payoff for me was being able to make changes in terms of what the screen needed. If the details section needed to disappear, I could go straight to details.visible. If a row needed a different cell, I could go straight to its case. I’d gladly take an extra class on a screen where those two changes used to send me through several data source methods.

Top comments (0)