Neil Macy

UITableView backgroundView as an Empty State

If you have a UITableViewController which can be empty, it's helpful to have some kind of empty state, rather than just an empty screen.

For example, if you have a to-do list app that's got no tasks, you might have a message telling the user that the list is empty. You could even have a button that allows them to create a task.

You can create an empty state with UITableView's backgroundView property, which can be any UIView.

For example, I have this line in my run tracking app:

tableView.backgroundView = runs.isEmpty ? NoRunsErrorView() : nil

It's expected that someone using the app would have at least one run in the list (otherwise why use a run tracking app?). So if they don't, I consider it to be an error state, and I have a custom view that I show in that case, with a prompt to check that the app has been granted the right HealthKit permissions.

Published on 3 January 2022