DEV Community

AJITHKUMAR K
AJITHKUMAR K

Posted on • Edited on • Originally published at dev.to

CollectionView

CollectionView is an object that presents the ordered collection of data items in the customizable layouts. It shows the data in the form of a grid layout. A collectionview is an instance of the UICollectionView class, which inherits the UIScrollView
The data associated with the collection view is treated as the individual items which can be grouped into the sections to be represented into a grid layout on the iPhone screen. The UICollectionView is used in most of the iOS applications

Methods:

dequeueReusableCell(withReuseIdentifier:for:)
is used to dequeue a reusable cell for a given index path. This method will either create a new cell or reuse an existing cell that is no longer visible, depending on whether or not a cell is already available for reuse.

register(_:forCellWithReuseIdentifier:)
is used to register a class or nib file for use in creating new cells.

reloadData()
is used to reload all of the data in the collection view, which causes the collection view to discard any existing cells and create new ones based on the current data.

Layouts

UICollectionViewFlowLayout is the default layout for a collection view, it arranges the items in a grid with optional header and footer views
UICollectionViewCompositionalLayout: Introduced in iOS13, it allow to create complex layouts using smaller composition layout units
UICollectionViewLayout is an abstract base class that you can subclass to create your own custom layouts.
It is necessary to set the layout for the collection view by assigning an instance of a UICollectionViewLayout subclass to the collection view's collectionViewLayout property.

You can customize the layout using properties such as itemSize, minimumLineSpacing, minimumInteritemSpacing, etc..

There is also the possibility to apply different layouts using setCollectionViewLayout(_:animated:), this allow to apply different layouts without reload the entire data.

Top comments (0)