Mark DiFranco walks through a quick overview of the UICollectionView user interface element, covering relevant classes, common terms, and when it should be used.
- [Instructor] So what is a UI Collection View? A good way to describe it is as a more generalized version of the UI Table View. Where a Table View shows items aligned only in the vertical axis, a Collection View allows you to arrange arbitrarily sized items vertically, horizontally, or even both. Collection Views can for example allow you to show products in a shopping cart, Collection Views also lend themselves nicely to larger screen sizes. They are great tools to use when looking to create a universal app on both iPhone and iPad.
Like Table Views, Collection Views can have sections to organize your data. Where Table Views refer to the content of the section as rows, Collection View sections instead contain items. There's even a specialized index path constructor for Collection Views. There is no functional difference between the two constructors, it is simply nicer to read when working with Collection Views. The positioning of items in a Collection View is determined by a UI Collection View Layout class. Every Collection View needs to have a Collection View layout in order to function properly.
UIKit provides a default layout we can use if we don't care too much about positioning items. This layout class is called UI Collection View Flow Layout. It exposes several properties as points of customization. You can however create your own layout. You could use a custom layout to achieve a layout similar to the Apple Watch app screen for example. Something to keep in mind is that you should only use a UI Collection View where a UI Table View will not suffice. Table View API's are more optimized than Collection Views so they require less code to be written.
For example, UI Table Views have built-in deletion UI whereas a Collection View would require you to implement it yourself. Next, we're going to take a look at how you can use a UI Collection View in your project.
- Populating cells
- Reloading items
- Animating insertions, deletions, and movements
- Configuring a UICollectionViewFlowLayout
- Interactively changing the layout
- Creating a custom UICollectionViewLayout