RPMouton
RPMouton

Reputation: 161

Type 'ViewController' does not conform to protocol 'UICollectionViewDataSource'

I am following a tutorial where using the UIPickerController to operate the camera. However when implementing UICollectionViewDatsaSource, I get an error saying that ViewController does not conform to the UICollectionViewDataSource protocol.

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate 

Any idea on how to fix this problem?

Upvotes: 16

Views: 20144

Answers (4)

Hemant Gupta
Hemant Gupta

Reputation: 51

You have to implement these two method in your ViewController class for Collection View :

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    <#code#>
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    <#code#>
}

Upvotes: 3

渠晓友
渠晓友

Reputation: 11

The UICollectionViewDataSource has two functions must be implemented!(they are required functions of the protocol).

To fix the problem,just implement the two functions like this: enter image description here

Upvotes: 0

Aks
Aks

Reputation: 8301

You must implement this two method in your ViewController class:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}

P.S. - Your function prototype should exactly match with above functions.(remove any '!' if present)

Upvotes: 33

zisoft
zisoft

Reputation: 23078

Adding the protocol definition to your custom class is not enough. You have to provide the required functions of the protocol. See the documenation of the protocol, you have to implement at least:

collectionView:numberOfItemsInSection:
collectionView:cellForItemAtIndexPath:

Upvotes: 2

Related Questions