Developers must manage two aspects of any CollectionView: First, they define the layout and presentation of the CollectionView. Second, they must update the data received through the dataSource object. This data includes both the view itself (presentation) and what’s in it (content). Similar to other processes managing layout, Collection Views enforce strict rules, which, when broken, result in a crash.

To add a CollectionView, developers cannot simply add a UICollectionView to their application and expect the desired content to display.

Using the layout object of UICollectionViewLayout, developers add cells, which represent interactive data, and supplementary views, such as headers, footers, or decorations adorning cells. This layout object does not define each individual view though. That process is done by the CollectionView’s data source object.

The data source of a CollectionView defines the content and view of each cell. Upon first loading content, the CollectionView creates a queue of view objects, which the developer will dequeue as required. New views must be registered with a unique identifier.

Then, for the actual content of the view, developers can prefetch cells and/or data. The former is an opt-out method because it provides a seamless scrolling experience. The latter, dealing with data, depends more on each developer and is often used when fetching data requires a costly request.

Maintaining CollectionViews in a responsive way, especially when reordering elements based on user input or using delegates to manage specific items in a view, is a tough process and can cause any number of errors.

For example, if you forget to register a new view or mix up a custom view with a normal cell type, your application will likely crash.

The main rule with CollectionViews is that the interface presenting data must match the order of the items in the data source object. This is managed automatically by the UICollectionViewDiffableDataSource object, though you might need to use other methods when rearranging or changing data.

Who we are

Embrace is a data driven toolset to help mobile engineers build better experiences. If you’d like to learn more about Embrace, you can check out our website or request a demo.