[英]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中我设置了
dataSource
和delegate
。 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.