开始练习Swift。在singleViewController
我试图制作UICollectionView
。在故事板中,我设置了dataSource
和delegate
。我在这里收到错误:
' 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]!
}
}
}
答案 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 。