Populating our photo grid
Now that we are maintaining a list of photos, we need to display it in our collection view. A collection view is populated when you provide a data source to it that implements its UICollectionViewDataSource
protocol. Probably, the most common thing to do is to let the view controller be the data source. We can do this by opening Main.storyboard
and control dragging from the collection view to the view controller:
After releasing the mouse, select dataSource
from the menu. After that, all we need to do is implement the data source protocol. The two methods we need to implement are collectionView:numberOfItemsInSection:
and collectionView:cellForItemAtIndexPath:
. The former allows us to specify how many cells should be displayed and the latter allows us to customize each cell for a specific index in our list. It is easy for us to return the number of cells that we want:
extension ViewController: UICollectionViewDataSource { func collectionView( collectionView...