How to make a simple collection view with Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A UICollectionView is the standard UIKit view for showing a grid or list of repeated items. Even a simple collection view needs three pieces: a layout, a data source, and a cell class. Once those are in place, the structure is straightforward.
The Smallest Useful Setup
For a beginner-friendly UIKit example, create a view controller that owns a collection view and provides some sample data.
The cell only needs a label and a reuse identifier.
Build the View Controller
Now create the collection view itself.
This produces a simple two-column grid.
What Each Part Does
The collection view needs a layout object to decide item positioning. Here that is UICollectionViewFlowLayout.
The data source answers two core questions:
- how many items exist
- what cell should appear for each item
The delegate layout method decides item size. Without that, the flow layout falls back to its default sizing behavior.
Registering and Reusing Cells
Collection views do not create a new view for every item permanently. They reuse cells as items scroll on and off screen.
That is why registration and dequeuing matter. You register the cell class once, then dequeue reusable cells inside cellForItemAt.
If the reuse identifier does not match, the app crashes. That is one of the most common beginner errors.
Storyboard Versus Code
You can build the same setup in Interface Builder, but doing it in code makes the moving parts more explicit:
- the layout is created directly
- the cell is registered directly
- the constraints are visible in one place
For learning the API, that clarity is useful.
Common Pitfalls
A common mistake is forgetting to set the data source. If dataSource is nil, no items appear even though the collection view itself is visible.
Another mistake is forgetting to register the cell class or nib before dequeuing it.
A third issue is miscalculating item width so the cells overflow or leave unexpected gaps. Test on different screen sizes and use the collection view width, not a hardcoded device width.
Summary
- A simple collection view needs a layout, a data source, and a reusable cell
- '
UICollectionViewFlowLayoutis the easiest starting point' - Register the cell class before dequeuing it
- Implement
numberOfItemsInSectionandcellForItemAtto show data - Use delegate sizing or layout configuration to control the grid appearance
Related reading
- How to make a simple rounded button in Storyboard?
- How to make a smooth image rotation in Android?
- how to make a specific text on TextView BOLD
- How to make a transparent UIWebView
- How to make a UILabel clickable?
- How to make a UILabel clickable?
- How to make an alert dialog fill 90 of screen size?
- How to make an Android device vibrate? with different frequency?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.