Create a new Cocoa Touch Class file (File > New > File... > iOS > Cocoa Touch Class). Name it MyCollectionViewCell. This class will hold the outlets for the views that you add to your cell in the storyboard.
We will connect this outlet later.
Open ViewController.swift and make sure you have the following content:
UICollectionViewDataSource and UICollectionViewDelegate are the protocols that the collection view follows. You could also add the UICollectionViewDelegateFlowLayout protocol to change the size of the views programmatically, but it isn't necessary.
We are just putting simple strings in our grid, but you could certainly do images later.
Setup the storyboard
Drag a Collection View to the View Controller in your storyboard. You can add constraints to make it fill the parent view if you like.
Make sure that your defaults in the Attribute Inspector are also
The little box in the top left of the Collection View is a Collection View Cell. We will use it as our prototype cell. Drag a Label into the cell and center it. You can resize the cell borders and add constraints to center the Label if you like.
Write "cell" (without quotes) in the Identifier box of the Attributes Inspector for the Collection View Cell. Note that this is the same value as let reuseIdentifier = "cell" in ViewController.swift.
And in the Identity Inspector for the cell, set the class name to MyCollectionViewCell, our custom class that we made.
Hook up the outlets
Hook the Label in the collection cell to myLabel in the MyCollectionViewCell class. (You can Control-drag.)
Hook the Collection View delegate and dataSource to the View Controller. (Right click Collection View in the Document Outline. Then click and drag the plus arrow up to the View Controller.)
Here is what it looks like after adding constraints to center the Label in the cell and pinning the Collection View to the walls of the parent.
Initialize a UICollectionView with a CGRect frame:
You can also create a UICollectionView in Interface Builder
Manage Multiple Collection view with DataSource and Flowlayout
Here we are managing multiple collection there delegate methods with didselect events.
Performing batch updates
You can animate complex changes to your collection view using the performBatchUpdates method. Inside the update block, you can specify several modifications to have them animate all at once.
Inside the update block, you can perform insertions, deletions, moves, and reloads. Here is how to determine which indexPath to use:
Index in new array
Index in old array
from: old array, to: new array
either new or old array (it shouldn't matter)
You should only call reload on cells that have not moved, but their content has changed. It is important to note that a move will not refresh the content of a cell, but only move its location.
To verify that your batch update will be performed correctly, make sure the set of indexPaths for deletion, move-from, and reload are unique, and the set of indexPaths for insertion, move-to, and reload are unique.
Here's an example of a proper batch update:
Swift - UICollectionViewDelegateFlowLayout
UICollectionView - Datasource
Every collection view must have a Datasource object. The Datasource object is the content that your app will display within the UICollectionView. At a minimum, all Datasource objects must implement the collectionView:numberOfItemsInSection: and collectionView:cellForItemAtIndexPath: methods.
UICollectionViewDelegate setup and item selection
Sometimes, if an action should be bind to a collection view's cell selection, you have to implement the UICollectionViewDelegate protocol.
Let's say the collection view is inside a UIViewController MyViewController.
In your MyViewController.h declares that it implements the UICollectionViewDelegate protocol, as below
In your MyViewController.swift add the following
The method that must be implemented is
As just an example we can set the background color of selected cell to green.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0