Quick nav

Old blog highlights (2003-2013)

iOS dev: declarative readable UITableViewControlllers

Search the docs or most online tutorials on building and populating a non-trivial static UITableView in iOS, such as the one used for a settings screen in your app, and you’ll start to notice a lot of duplication and switches over enums and cell types. Here’s what I mean:

SimpleRow and SectionType are simple data structures used for type clarity. The methods pointed to by the selectors implement the actual logic of displaying data and handling the tap on the cells (see below).

The code above is certainly not foolproof, but it gets the job done without any constants or assumptions on the number of sections, cells, their ids, etc.

By splitting the functionality into a tiny view controller and a contents array which describes the sections, rows, and their wanted behavior (when initializing them and when selecting), we achieved a concise, clear, reasonably flexible and easily reusable code for static tables with basic cells.

If this helps give coding ideas to at least one other dev, I’ll be ecstatic. Remember to drop some feedback on twitter: @alexbrie