Static

How many static table view based UIs have you developed in your career as an iOS developer? More than half the apps I worked on had at least one screen made up like that.

Handling static table views using Interface Builder might seem smart at first, but it soon becomes a nightmare when it is time to update the designs. Doing it in code is more flexible.

When dealing with static table views of a certain scale most developers end up rolling out their own model to static table view framework, feeling like they are reinventing the wheel every time. Static puts an end to that.

Made by the team at Venmo, which is pretty active in the open source community, this framework allows you to describe your static table view in a highly configurable way, and takes care of all the UITableViewDataSource and UITableViewDelegate implementation details.

Let’s have a look at some code examples. As with all the other posts in The iOS Times Advent Calendar series, you can see a complete example on GitHub.

How to configure a table view using Static

Say that you have a UIViewController class, with a UITableView subview, configuring it using Static is as easy as:

import UIKit
import Static

class ViewController: UIViewController {

  @IBOutlet weak var tableView: UITableView!

  let dataSource = DataSource()

  override func viewDidLoad() {
    super.viewDidLoad()

    dataSource.tableView = tableView
    dataSource.sections = [
      Section(
        header: Section.Extremity.Title("title"),
        rows: [
          Row(text: "foo"),
          Row(text: "bar"),
        ]
      )
    ]
  }
}

DataSource is the core of Static, and it has two configurable properties: tableView, the table view to configure, and sections, an array of sections configuration for the table.

Static’s API is very descriptive, and Swifty. Let’s see another example:

let data = [
	Section(
		header: Section.Extremity.Title("title"),
		rows: [
			Row(text: "foo"),
			Row(text: "bar"),
		]
	),
	Section(
		rows: [
			Row(text: "text only"),
			Row(detailText: "detail text only"),
			Row(
				text: "look at me",
				image: UIImage(contentsOfFile: "img.png),
			)
		],
		footer: Section.Extremity.Title("This is the footer")
	)
]

All the arguments in the Section and Row initializers are either preset or optional, which makes it really easy to init these objects inline. And there is more, thanks to Swift’s awesomeness and all the arguments having a default, the arguments order doesn’t matter, you can pass them in whichever order you like.

How to add a row selection handler

Most of the times you will need to have some kind of user interaction in your static table views. Easy peasy.

let data = [
  Section(
    Row(
      title: "Show new screen",
      accessory: .DisclosureIndicator,
      selection: {
        // Logic to show the new screen, for example
        self.performSegueWithIdentifier("new-screen", sender: nil)
      }
    )
  )
]

Next Steps

The iOS Times Advent Calendar’s post certainly don’t aim to be comprehensive tutorials on how to use a library, but rather provide a little taste of it, to ignite your curiosity.

If you think you could simplify your static table views logic using Static and stop rewriting generators once and for all, head over to the project’s repo on GitHub, or feel free to play around with the code from this tutorial.


That’s it for today. See you tomorrow with a library that will revolutionise your navigation bar’s behaviour. Subscribe to the email list to avoid missing out.

If you found this post useful and want to support the Advent Calendar please consider sharing it on your favourite social network. Thanks 🎅