连接一个CollectionView到一个ViewController,在连接dataSource-Swift-XCode时出错

我可以连接委托,但是,当连接数据源时,我收到以下错误:

2015-07-13 16:45:17.529 paintApp [4628:168318] – [UIView collectionView:numberOfItemsInSection:]:无法识别的select器发送到实例0x7f8a12651c60 2015-07-13 16:45:17.539 paintApp [4628:168318] *终止应用程序由于未捕获exception'NSInvalidArgumentException',原因:' – [UIView collectionView:numberOfItemsInSection:]:无法识别的select器发送到实例0x7f8a12651c60'*第一个抛出调用堆栈:(0 CoreFoundation 0x0000000107f67b75 exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000109cf7bb7 objc_exception_throw + 45 2的CoreFoundation 0x0000000107f6ec8d – [NSObject的(NSObject的)doesNotRecognizeSelector:] + 205 3的CoreFoundation 0x0000000107ec66fc ___forwarding_ + 988 4的CoreFoundation 0x0000000107ec6298 _CF_forwarding_prep_0 + 120 5的UIKit 0x00000001090a5286 – [UICollectionViewData _updateItemCounts] + 296 6的UIKit 0x00000001090a7469 – [UICollectionViewData numberOfSections] + 22 7的UIKit 0x000000010909158e – [UICollectionViewFlowLayout _getSizingInfos] + 348 8 UIKit 0x000000010909268 9 – [UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 526 9的UIKit 0x000000010908deff – [UICollectionViewFlowLayout prepareLayout] + 257 10的UIKit 0x00000001090a53f0 – [UICollectionViewData _prepareToLoadData] + 67 11的UIKit 0x00000001090a5ac9 – [UICollectionViewData validateLayoutInRect:] + 54 12的UIKit 0x000000010906e198 – [UICollectionView layoutSubviews] + 170 13 UIKit的0x0000000108ab3303 – [UIView的(CALayerDelegate)layoutSublayersOfLayer:] + 521 14 QuartzCore 0x000000010d463de8 – [CALayer的layoutSublayers] + 150 15 QuartzCore 0x000000010d458a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 16 QuartzCore 0x000000010d45887e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 17 QuartzCore 0x000000010d3c663e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242 18 QuartzCore 0x000000010d3c774a _ZN2CA11Transaction6commitEv + 390 19的UIKit 0x0000000108a3795d – [UIApplication _reportMainSceneUpdateFinished:] + 44 20 UIKit 0x0000000108a3866a – [UIApplication _runWithMa inScene:transitionContext:完成:] + 2684 21的UIKit 0x0000000108a37005 – [UIApplication的workspaceDidEndTransaction:] + 179个22 FrontBoardServices 0x000000010c58d253 31- [FBSSerialQueue performAsync:] _ block_invoke + 16 23的CoreFoundation 0x0000000107e9c9bc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 12 24的CoreFoundation 0x0000000107e92705 __CFRunLoopDoBlocks + 341 25的CoreFoundation 0x0000000107e924c5 __CFRunLoopRun + 2389 26 CoreFoundation 0x0000000107e91906 CFRunLoopRunSpecific + 470 27 UIKit 0x0000000108a36a72 – [UIApplication _run] + 413 28 UIKit 0x0000000108a39870 UIApplicationMain + 1282 29 paintApp 0x0000000107d7272e top_level_code + 78 30 paintApp 0x0000000107d7276a main + 42 31 libdyld.dylib 0x000000010a4d3145 start + 1 32 ??? 0x0000000000000001 0x0 + 1)libc ++ abi.dylib:以NSException(lldb)types的未捕获exception终止

这里是我的视图控制器类:

import UIKit class ViewControllerGallery: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { var tableData: [String] = ["img1" , "img2"] var tableImages: [String] = ["file1.png", "file2.png"] var collectionView: UICollectionView! override func viewDidLoad() { super.viewDidLoad() collectionView = UICollectionView(frame: self.view.frame) collectionView.dataSource = self collectionView.delegate = self collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") self.view.addSubview(collectionView) // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return tableData.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell : ColViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath : indexPath) as ColViewCell cell.labelCell.text = tableData[indexPath.row] cell.imgCell.image = UIImage(named : tableImages[indexPath.row]) return cell //UICollectionViewCell() } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { println("Cell \(indexPath.row) selcted") } } 

这里是我的“单元格”类,其中单元格图像和标签连接到:

 import UIKit class ColViewCell: UICollectionViewCell { @IBOutlet var imgCell: UIImageView! @IBOutlet var labelCell: UILabel! } 

你的错误已经足够自我解释了。 在你设置collectionView.dataSource地方,你有一个错误源 – 而不是实现UICollectionViewDataSource的东西,你设置UIView实例。

我想,你需要类似的东西

 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. collectionView.dataSource = self; }