How to create a UICollectionView with multiple sections in Swift

Oscar de la Hera Gomez
A flower that represents Swift next to a flower that represents XCode. Beneath it sits the text that states 'Multi Section UICollectionView'.

A step by step tutorial on creating a UICollectionView with multiple sections in Swift (iOS). Github repository included.

The following tutorial walks you through how to create a UICollectionView with multiple sections in Swift (iOS) using XCode. The project builds on our Open Source Swift project and implements a UICollectionView with three custom UICollectionViewCells.

This tutorial starts on the main branch and the changes are available on the tutorial/multi-section-collectionview branch of the repository found below.

Please note this tutorial uses the TinyConstraints Swift package, which we added using the Swift Package Manager.

What are we creating in this tutorial ?

A screenshot of what we are making in this tutorial.

In this tutorial we will be creating a two section UICollectionview that is illustrated in the image above.

If you are interested in a tutorial for a carousel please follow the tutorial linked below.

To learn how to do this exact tutorial using Apple's Modern UICollectionView methodology, follow the tutorial below.

Tutorial

We recommend that you clone our Open Source Swift Starter Project, checking out the main branch and carrying out the steps below. The changes can be found on the tutorial/multi-section-collectionview branch.

git clone git@github.com:delasign/swift-starter-project.git

Please note that we are creating this multi-section UICollectionView in the CustomUIView that we setup as part of our Open Source Swift starter project; which follows our structured UIView methodology.

Step One: Create the content

A screenshot showing you how we created the content for the tutorial.

First, you must create the content required for our sample titles and body.

To do this:

  • Update or create a new struct in UIContent.Swift found under Models - > LanguageContent -> Content.
  • Update the English (en) and Spanish strings (es) found under Models - > LanguageContent -> Strings.

For more information, please consult the tutorial linked below.

Step Two: Create the header style

A screenshot of the code for the header attributed style.

Under the Coordinators folder, within the Styleguide folder, create a new file called Styleguide+Header.swift and paste in the code found below.

For more information on Styleguide and Styles, please use the links below.

Please note that we updated the Fonts.swift file to include Helvetica Neue Bold. Update the code in Models -> Constants -> Fonts.swift to the code in the snippet below.

Step Three: Create the cell title style

A screenshot of the code for the cell title attributed style.

Under the Coordinators folder, within the Styleguide folder, create a new file called Styleguide+CellTitle.swift and paste in the code found below.

Step Four: Create the cell body style

A screenshot of the code for the cell body attributed style.

Under the Coordinators folder, within the Styleguide folder, create a new file called Styleguide+CellBody.swift and paste in the code found below.

Step Five: Create the title cell

A screenshot of the code for the Title UICollectionViewCell.

Under the CustomUIView folder, create a new folder called Components.

Within the Components folder, create another folder called TitleCell.

Within the TitleCell folder, create another file called TitleCell.swift and paste in the code found below.

Please note that the setupUI function is created in the next step.

Step Six: Create the title cell UI extension

A screenshot of the code for our Title UICollectionViewCell UI extension.

Within the TitleCell folder, create another file called TitleCell+UI.swift and paste in the code found below.

Step Seven: Create the title cell update extension

A screenshot of our Title UICollectionViewCell update extension.

Within the TitleCell folder, create another file called TitleCell+Update.swift and paste in the code found below.

Step Eight: Create the cell for section A

A screenshot of the code four our Section A UICollectionViewCell.

Within the previously created Components folder, create another folder called SectionACell.

Within the SectionACell folder, create another file called SectionACell.swift and paste in the code found below.

Please note that the setupUI function is created in the next step.

Step Nine: Create the section A cell UI extension

A screenshot of the code for our Section A UICollectionViewCell UI extension.

Within the SectionACell folder, create another file called SectionACell+UI.swift and paste in the code found below.

Step Ten: Create the section A cell update extension

A screenshot of the code of our Section A UICollectionViewCell update extension.

Within the SectionACell folder, create another file called SectionACell+Update.swift and paste in the code found below.

Step Eleven: Create the cell for section B

A screenshot of the code for our Section B UICollectionViewCell.

Within the previously created Components folder, create another folder called SectionBCell.

Within the SectionBCell folder, create another file called SectionBCell.swift and paste in the code found below.

Please note that the setupUI function is created in the next step.

Step Twelve: Create the section B cell UI extension

A screenshot of the code of our UICollectionViewCell UI extension.

Within the SectionBCell folder, create another file called SectionBCell+UI.swift and paste in the code found below.

Step Thirteen: Create the section B cell update extension

A screenshot of the code for our Section B UICollectionViewCell update extension.

Within the SectionBCell folder, create another file called SectionBCell+Update.swift and paste in the code found below.

Step Fourteen: Declare variables

A screenshot of the declaration of variables for our UICollectionView, sections and items.

In CustomUIView.swift, declare your UICollectionView and section constants.

In the case of UICollectionViews, we like to declare and set them up functionally, as demonstrated in the snippet below.

We used an enum to identify sections, this will be useful later on in Step Sixteen, where we declare the datasource.

Please note that we have also removed the label from the UI declarations as well as all associated functionality for this tutorial.

Step Fifteen: Setup your UICollectionView

A screenshot of the code that we used to setup the UICollectionView.

In CustomUIView+UI.swift replace the code with the snippet below.

Please note that the datasource and delegate functionality are added in the next step.

Step Sixteen: Add the delegate and datasource functionality

A screenshot of the code for our UICollectionView delegate and datasource.

Under the CustomUIView folder, create a new file called CustomUIView+CollectionView.

Within this, paste in the code found below.

We recommend that you place it under the +Notifications extension and that you create a function for each cell type within your colllectionview and returning these cells, as show between line 34 -> 101.

Step Seventeen: Add the visual parameters functionality

A screenshot of the visual parameter functionality for our UICollectionView.

Within CustomUIView+CollectionView.swift, below the delegate and datasource functionality, add the following code.

Step Eighteen: Reload UICollectionView on content update

A screenshot of the how we reloaded the data for our UICollectionView when the content is updated.

To make sure that the UICollectionView loads, in CustomUIView+Update, replace the onContentUpdate function with the following code.

A screenshot of how we invalidated the layout of the UICollectionView when the views are laid out through the layoutSubviews function.

In CustomUIView.swift, add the following code to your lifecycle, below the removeFromSuperview functionality.

Step Twenty: Verify

A screenshot of the final product in landscape and portrait.

Run your code to see your results. It should look as above.

Any Questions

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.

delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details