Nicolas Rudisky
Nicolas Rudisky

Reputation: 193

Type does not conform to protocol Swift

Started practicing Swift. In singleViewController I am trying to make a UICollectionView. In storyboard I set the dataSource and delegate. Here I am getting the error:

'UICollectionView' does not conform to protocol 'UICollectionViewDataSource'

import UIKit

class galeriacontroler: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{

    @IBOutlet weak var collectionview: UICollectionView!

    let fotosgaleria = [UIImage(named: "arbol3"), UIImage(named:"arbol4")]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.fotosgaleria.count
    }

    func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellImagen", forIndexPath:indexPath) as! cellcontroler

        cell.imagenView2?.image = self.fotosgaleria[indexPath.row]
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("showImage", sender: self )
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showImage"
        {
            let indexPaths = self.collectionview!.indexPathsForSelectedItems()
            let indexPath = indexPaths![0] as NSIndexPath

            let vc = segue.destinationViewController as! newviewcontroler

            vc.image = self.fotosgaleria[indexPath.row]!
        }
    }
}

Upvotes: 1

Views: 1144

Answers (2)

user4886069
user4886069

Reputation:

When you import UICollectionViewDataSource you must implement cellForItemAtIndexPath method

Add the following method to your code:

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

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imagesCellIdentifier", forIndexPath:indexPath) as! cellcontroler
cell.secondImageView?.image = self.photosGalleryArray[indexPath.row]

return cell
}

willDisplayCell is not necessary to implement after this.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726589

UICollectionViewDataSource has two required methods - collectionView(_:numberOfItemsInSection:) and collectionView(_:cellForItemAtIndexPath:), of which you have implemented only one.

You need to add an implementation for collectionView(_:cellForItemAtIndexPath:) to fix this problem:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell {
    var  cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as CollectionCell
    ... // Do more configuration here
    return cell
}

Upvotes: 2

Related Questions