2️⃣UICollectionView Setup
Fall 2023 | Vin Bui
Create a Custom UICollectionViewCell
Creating a UICollectionViewCell
is very similar to creating a UITableViewCell
. However, there are just minor syntax changes. Follow these steps:
Only Steps 1 and 2 are different when creating a UICollectionViewCell
compared to a UITableViewCell
Our custom class needs to be a subclass of
UICollectionViewCell
class CustomCollectionViewCell: UICollectionViewCell { }
Create the following initializer:
Determine what views to create and write a helper function to initialize its properties.
For example, if we need to display some text, we would create a
UILabel
and create a helper function to initialize its font, font color, etc. Note that we do not know anything about the data yet, so the property.text
of theUILabel
will not be initialized yet.
Inside of the helper function, add the views we created as a subview to
contentView
and constrain the view with respect tocontentView
. Then call the helper function inside of the initializer.This is one of the main differences from what we have been doing before. Instead of referencing
view
, we will be usingcontentView
. Note: we do not need to usesafeAreaLayoutGuide
here.
Create a
configure
function (do not makeprivate
) that will take in some data as a parameter, and configure our views.For example, we could write a function that takes in a
String
and setsUILabel.text
property equal to the value passed in.
Create a reuse identifier for this cell:
static let reuse = "<reuse_identifier>"
See “Dequeuing Cells” below for more information.
Dequeuing Cells
The idea behind this concept is the exact same for that of a UITableViewCell
. Read it here:
Setting Up a UICollectionView
A UICollectionView
is just like any other UIView
that we've worked with thus far. We've initialized the view by doing the following steps:
Create the view
Configure the view by changing its properties
Adding the view as a subview to some parent view
Enable auto layout and set up constraint
With a UICollectionView
, we do the exact same thing but with additional steps:
Register a
UICollectionViewCell
For example, if we had a custom class called
CustomCollectionViewCell
with a static reuse constant calledreuse
, we would use the following code:
Set the
UICollectionView
delegate (create an extension just like any other protocol)See UICollectionViewDelegate section below
Set the
UICollectionView
dataSource (create an extension just like any other protocol)See UICollectionViewDataSource section below
Every step that we mentioned above is very similar to that of a UITableViewCell
; however, there is 1 more additional step.
Initialize the collection view with a
UICollectionViewFlowLayout
and conform toUICollectionViewDelegateFlowLayout
See UICollectionViewFlowLayout section below
6: UICollectionViewDelegate
The purpose of a UICollectionViewDelegate
is to add functionality to the collection view. A class conforming to the protocol UICollectionViewDelegate
does not have any required functions to implement; however, the most common function to implement is: didSelectItemAt
.
7: UICollectionViewDataSource
In contrast to UICollectionViewDelegate
, there are two required functions to implement: cellForItemAt
and numberOfItemsInSection
. The idea is exactly the same as that of a table view, but with minor syntax changes (”item” instead of “row”).
Read the details here:
8: UICollectionViewFlowLayout
Everything we’ve mentioned earlier is very similar to a table view but with minor syntax changes. However, there is one more additional step that is required by a collection view that gives it customization benefits.
Create a FlowLayout and Initialize CollectionView
Inside of the helper function that sets up the collection view, add the following lines of code:
Let’s go over this line by line:
Create a
UICollectionViewFlowLayout
instance(REQUIRED) Set the collection view’s scroll direction:
.vertical
or.horizontal
(OPTIONAL) Set the spacing between each line (top and bottom)
(OPTIONAL) Set the spacing between each item (left and right)
Initialize CollectionView with the layout we just created
Conform to UICollectionViewDelegateFlowLayout
In the previous step, we configured the collection view’s layout. Remember how the UICollectionViewDelegate
did not have a heightForRowAt
function like a table view does? Well that’s because each item (cell) has a customizable height and width whereas in a table view, we could only customize the row’s height. To do this, just create an extension and conform to UICollectionViewDelegateFlowLayout
and add this function:
If we want to configure the size of each item relative to the size of the collection view, we can use collectionView.frame
. For example, if we want the item cell to be half of the width of the collection view, we can do collectionView.frame.width / 2
.
Complete Code
Comparing with a UITableView
A lot of the material mentioned above is very repetitive and already seen in a UITableView
. For comparison purposes, here are the main differences between the two when setting them up:
Create a subclass of
UICollectionViewCell
insteadThe
init
function is different
A flow layout is required when initializing the collection view
You may have noticed that instead of using
private let collectionView = UICollectionView()
we usedprivate var collectionView: UICollectionView!
. The reason for this is because we have to pass in a layout when initializing the collection view. By replacing it with this line, we are making a promise that we will initialize the collection view later. (There is a cleaner way to do this but it is a bit advanced for now).
You must conform to
UICollectionViewDelegateFlowLayout
Implement the
sizeForItemAt
function
Syntax: use “items” instead of “rows”
Ex:
cellForItemAt
instead ofcellForRowAt
. However, the implementation is the exact same.
Tip: If we start typing the function name such as cellForItemAt
, we can choose which one we want and Xcode will autofill the function header for us!
Last updated