Lesson 17

Collection Views

In the previous lesson, you learned about table views. Collection views are similar to table views in many respects; the primary difference between them is that collection views are not restricted to single column layouts. A collection views layout can be customized programmatically, allowing collection views to present data in grid layouts, circular layouts, and cover-flow layouts (see Figure 17.1).

Screenshot of a iOS screen with Collection view presenting data in a grid layout with Li, Al, Si in the first row, Hg in the second row, and N, O, F in the third row.

Figure 17.1

Collection views are instances of the UICollectionView class and are part of the UIKit framework. Data in a collection view is referred to as Items that are grouped into sections. Each section can have an optional header and a footer view.

Creating a Collection View with Interface Builder

To add a new scene in your storyboard that contains a collection view, simply drag and drop a Collection View Controller object into your scene. A collection view controller is an object that manages a collection view (see Figure 17.2).

Screenshot of a storyboard screen with Collection View Controller encircled with an arrow pointing to a scene.

Figure 17.2

If, however, you want to add a collection view to an existing scene, drag and drop a collection view object onto the scene. When using a collection view your view controller class will be responsible for managing the collection view and will need to implement relevant methods from the UICollectionViewDataSource ...

Get Swift iOS 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.