类型不符合协议Swift

时间:2016-05-17 17:03:02

标签: ios swift uicollectionview

开始练习Swift。在singleViewController我试图制作UICollectionView。在故事板中,我设置了dataSourcedelegate。我在这里收到错误:

  

' UICollectionView'不符合协议' 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 个答案:

答案 0 :(得分: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 :(得分:1)

导入 UICollectionViewDataSource 时,必须实现 cellForItemAtIndexPath 方法

将以下方法添加到您的代码中:

myweb
  |- app/
  |   |-- view/
  |   |   |-- login.php
  |   |-- base.php
  |
  |- src/
  |   |-- controller/
  |   |   |-- AccessController.php
  |
  |- model/
  |   |-- Router.php
  |
  |- index.php
  |- .htaccess
在此之后不需要

willDisplayCell