You have already set up a flow layout for your collection view, but you don’t know how to render cells in your collection view.
Either use the
UICollectionViewCell class directly to present
your cells, or subclass this class and provide further implementation on
top of that class. In addition, you can have a .xib
file associated with your cell, as we will soon see.
Let’s take this one step at a time and start with the fastest and
easiest way of creating our cells: instantiate objects of type
UICollectionViewCell and feed them to our
collection view in our data source. The
class has a content view property named
contentView, where you can add your own views
for display. You can also set various other properties of the cell, such
as its background color, which is what we are going to do in this
example. But before we begin, let’s first set the expectations of what
we are going to achieve in this example code and explain the
We are going to program a collection view with a flow layout that displays three sections, each of which contains anywhere between 20 and 40 cells, with the first section’s cells all being red, the second section’s cells all being green, and the third section’s cells all being blue, as shown in Figure 5-5.
So let’s get started. ...