Satre
Satre

Reputation: 1744

Error with UICollectionViewDataSource in Swift

I am trying to implement a Collection view that is wired to a collection view in the interface builder as an IBOutlet. As required, I am trying to declare the UIViewController class file that I have created for the controller that contains the Collection View as a UICollectionViewDataSource.

However, Xcode keeps giving me an error that the view controller doesn't conform to the protocol of UICollectionViewDataSource, despite it being declared right there.

I am setting the controller as the datasource and delegate like so:

ObjectsCollection.delegate = self
ObjectsCollection.dataSource = self

Here is s screenshot of the error:

screen shot of error

What is the cause of the error? I can't seem to find it.

Upvotes: 1

Views: 2397

Answers (2)

rob mayoff
rob mayoff

Reputation: 385998

The UICollectionViewDataSource protocol defines two required methods:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

You are missing one or both of them.

Here's how you can see what you're missing:

  1. Go to the menu bar and select View > Navigators > Show Issue Navigator (default shortcut: ⌘4).

  2. In the Issue Navigator, click on the disclosure triangles to turn them down until the navigation shows you the missing required functions. Or, option-click on the top triangle until it opens all triangles in the tree.

Example:

show issues

Upvotes: 5

mustafa
mustafa

Reputation: 15464

The thing is if you declared your class will conform a protocol you have to implement required methods from that protocol.

Upvotes: 0

Related Questions