简体   繁体   English

类型不符合协议Swift

[英]Type does not conform to protocol Swift

Started practicing Swift. 开始练习Swift。 In singleViewController I am trying to make a UICollectionView . singleViewController我正在尝试创建一个UICollectionView In storyboard I set the dataSource and delegate . 在storyboard中我设置了dataSourcedelegate Here I am getting the error: 我在这里得到错误:

'UICollectionView' does not conform to protocol 'UICollectionViewDataSource' '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]!
        }
    }
}

UICollectionViewDataSource has two required methods - collectionView(_:numberOfItemsInSection:) and collectionView(_:cellForItemAtIndexPath:) , of which you have implemented only one. UICollectionViewDataSource有两个必需的方法 - collectionView(_:numberOfItemsInSection:)collectionView(_:cellForItemAtIndexPath:) ,其中只实现了一个。

You need to add an implementation for collectionView(_:cellForItemAtIndexPath:) to fix this problem: 您需要为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
}

When you import UICollectionViewDataSource you must implement cellForItemAtIndexPath method 导入UICollectionViewDataSource时 ,必须实现cellForItemAtIndexPath方法

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. 之后不需要执行willDisplayCell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift - 类型“*”不符合协议“*” - Swift - Type '*' does not conform to protocol '*' 类型不符合协议序列类型 - Swift - type does not conform to protocol Sequence Type - Swift 类型“NSPersistentStore”在swift中不符合协议“BooleanType” - Type 'NSPersistentStore' does not conform to protocol 'BooleanType' in swift Swift:类型'ViewController'不符合协议'UIPageViewControllerDataSource' - Swift: Type 'ViewController' does not conform to protocol 'UIPageViewControllerDataSource' Swift 2.0类型'()'不符合协议 - Swift 2.0 Type '()' does not conform to protocol Swift:`类型'[String]'不符合协议'StringLiteralConvertible' - Swift: `Type '[String]' does not conform to protocol 'StringLiteralConvertible'` 类型“myViewController”不符合Swift中的协议UIPIckerDataSource - Type “myViewController” does not conform to protocol UIPIckerDataSource in Swift Swift - MultipeerConnectivity类型不符合协议 - Swift - MultipeerConnectivity Type does not conform to protocol Swift-类型'MenuViewController'不符合协议'GKGameCenterControllerDelegate' - Swift - Type 'MenuViewController' does not conform to protocol 'GKGameCenterControllerDelegate' Swift - 结构类型不符合协议 - Swift - struct type does not conform to protocol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM