Search code examples
swiftuicollectionviewuicollectionviewcelluicollectionviewdelegate

What is the difference between a UICollectionView Item, Section, and cell?


I am implementing a UICollectionView with dynamic number of buttons/cells in it. I am trying to understand the difference between each (Item, Section, and cell), so I can understand the collectionView methods provided by Apple.


Solution

  • Consider the Photos app. Sections are separated by "supplementary views" which correspond to the labels "Yesterday", "Wednesday", etc. in Photos app. Each section consists of multiple (or perhaps none) "cells". What we call "items" usually have one-to-one correspondance with those cells.

    So think of items as data entities you want to put in a collection view as cells. If those items can be grouped according to certain criteria, you can put those cells in a same section, and if there are more than one group, create multiple sections.

    Each cell in a collection view has a corresponding index called IndexPath, so that it can be properly referenced with your data source. In a nutshell, IndexPath is a combination of section and row. row is zero-based indexed, and starts from zero in each section. section is also zero-based. For instance, if you want to find a cell in the third row of the second section, use IndexPath(row: 2, section: 1).

    You should take a look at Apple documentation.