Implementing layoutAttributesForItem(_:)

You're almost ready to make use of your beautiful custom layout. Just a few more methods remain to be tackled. The method you need to implement next is layoutAttributesForItem(_:). This method receives the IndexPath for a single item, and the layout is supposed to return the correct layout attributes for it. The following is the implementation:

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { 
return layoutAttributes[indexPath.row]
}

This implementation is nice and clean; once again, we can thank the preparation we did earlier. You can simply use the row property of the IndexPath to retrieve the required layout attributes from the stored array of attributes.