2016-05-17 2 views
1

Начал практиковать Swift. Я хочу сделать UICollectionView. В раскадровке я установил dataSource и delegate. Здесь я получаю сообщение об ошибке:Тип не соответствует протоколу Swift

'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]! 
     } 
    } 
} 

ответ

2

UICollectionViewDataSource имеет два обязательных метода - collectionView(_:numberOfItemsInSection:) и collectionView(_:cellForItemAtIndexPath:), из которых вы осуществили только один.

Вы должны добавить реализацию для collectionView(_:cellForItemAtIndexPath:), чтобы решить эту проблему:

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

При импорте UICollectionViewDataSource вы должны реализовать cellForItemAtIndexPath метод

Добавьте следующий метод к коду:

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 не требуется для реализации после этого.

Смежные вопросы