Connecting the dataSource and the delegate

You have probably noticed that when we created collectionview, we set View Controller itself as dataSource and delegate:

collectionView.dataSource = self
collectionView.delegate = self

A common pattern found in Cocoa in many components is the delegate pattern, where part of the behavior is delegated to another object, and that object must implement a particular protocol.

In the case of UICollectionView, and likewise for UITableView, we have to delegate one of the class references to provide the content for the view, dataSource, and the other to react to events from the view itself. In this way, the presentation level is completely decoupled from the data and the business logic, which reside in two specialized ...

Get Swift: Developing iOS Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.