Chapter 12. Table Views and Collection Views

One of the most common tasks for any app, regardless of platform, is displaying lists or collections of data. iOS and OS X provide a number of tools for viewing data, and in this chapter you’ll learn how to use them.

Both iOS and OS X feature table views and collection views. A table view is designed to provide a list of data, while a collection view is designed to show a grid of data. Both table views and collection views can be customized to provide different layouts.

Table views are used all over OS X—Finder and iTunes both use it to show lists of files and songs. Table views are used even more heavily in iOS—any time you see a vertically scrolling list, such as the list of messages in Messages or the options in Settings, you’re seeing a table view.

Collection views are used a little less frequently, as they’re a newer addition to both platforms. Collection views can be seen (again) in Finder and iTunes, as well as in Launchpad. On the iPad, a collection view appears in the Clock application, which was added in iOS 6.

Data Sources and Delegates

Despite their differences in layout, table and collection views have very similar APIs. When a data display view prepares to show content, it has to know the answers to at least two questions:

  • How many items am I showing?
  • For each item, what do I need to do to display it?

These questions are asked of the view’s data source, which is an object that conforms to the table view’s data source protocol ...

Get Swift Development with Cocoa now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.