将CollectionView连接到ViewController,连接dataSource-Swift-XCode时出现错误 [英] Connecting a CollectionView to a ViewController, Getting Error when connecting dataSource-Swift-XCode

查看:30
本文介绍了将CollectionView连接到ViewController,连接dataSource-Swift-XCode时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以连接委托,但是,在连接数据源时出现以下错误:

I can connect the delegate, however, when connecting the data source I am getting the following error:

2015-07-13 16:45:17.529 paintApp [4628:168318]-[UIView collectionView:numberOfItemsInSection:]:无法识别的选择器已发送到实例0x7f8a12651c602015-07-13 16:45:17.539 paintApp [4628:168318] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[UIView collectionView:numberOfItemsInSection:]:无法识别的选择器已发送至实例0x7f8a12651c60'* :第一个引发调用堆栈:(0 CoreFoundation 0x0000000107f67b75 例外预处理+ 1651个libobjc.A.dylib 0x0000000109cf7bb7 objc_exception_throw + 452 CoreFoundation 0x0000000107f6ec8d-[NSObject(NSObject)didNotRecognizeSelector:] + 2053 CoreFoundation 0x0000000107ec66fc ___ forwarding _ + 9884 CoreFoundation 0x0000000107ec6298 _CF_forwarding_prep_0 + 1205 UIKit 0x00000001090a5286-[UICollectionViewData _updateItemCounts] + 2966 UIKit 0x00000001090a7469-[UICollectionViewData numberOfSections] + 227 UIKit 0x000000010909158e-[UICollectionViewFlowLayout _getSizingInfos] + 3488 UIKit 0x0000000109092689-[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 5269 UIKit 0x000000010908deff-[UICollectionViewFlowLayout prepareLayout] + 25710 UIKit 0x00000001090a53f0-[UICollectionViewData _prepareToLoadData] + 6711 UIKit 0x00000001090a5ac9-[UICollectionViewData validateLayoutInRect:] + 5412 UIKit 0x000000010906e198-[UICollectionView layoutSubviews] + 17013 UIKit 0x0000000108ab3303-[UIView(CALayerDelegate)layoutSublayersOfLayer:] + 52114 QuartzCore 0x000000010d463de8-[CALayer layoutSublayers] + 15015 QuartzCore 0x000000010d458a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 38016 QuartzCore 0x000000010d45887e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 2417 QuartzCore 0x000000010d3c663e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 24218 QuartzCore 0x000000010d3c774a _ZN2CA11Transaction6commitEv + 39019 UIKit 0x0000000108a3795d-[UIApplication _reportMainSceneUpdateFinished:] + 4420 UIKit 0x0000000108a3866a-[UIApplication _runWithMainScene:transitionContext:completion:] + 268421 UIKit 0x0000000108a37005-[UIApplication WorkspaceDidEndTransaction:] + 17922 FrontBoardServices 0x000000010c58d253 31- [FBSSerialQueue performAsync:] _ block_invoke + 1623 CoreFoundation 0x0000000107e9c9bc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 1224 CoreFoundation 0x0000000107e92705 __CFRunLoopDoBlocks + 34125 CoreFoundation 0x0000000107e924c5 __CFRunLoopRun + 238926 CoreFoundation 0x0000000107e91906 CFRunLoopRunSpecific + 47027 UIKit 0x0000000108a36a72-[UIApplication _run] + 41328 UIKit 0x0000000108a39870 UIApplicationMain + 128229 paintApp 0x0000000107d7272e top_level_code + 7830 paintApp 0x0000000107d7276a主+ 4231 libdyld.dylib 0x000000010a4d3145开始+ 132 ???0x0000000000000001 0x0 + 1)libc ++ abi.dylib:以类型为NSException的未捕获异常终止(lldb)

2015-07-13 16:45:17.529 paintApp[4628:168318] -[UIView collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x7f8a12651c60 2015-07-13 16:45:17.539 paintApp[4628:168318] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x7f8a12651c60' * First throw call stack: ( 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 0x0000000109092689 -[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 _runWithMainScene:transitionContext:completion:] + 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: terminating with uncaught exception of type NSException (lldb)

这是我的视图控制器类:

Here is my view controller class :

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")
}

}

这是我的单元格"课程,其中已将单元格图像和标签连接到:

Here is my 'cell' class where have connected the cells image and label to:

import UIKit

class ColViewCell: UICollectionViewCell {

@IBOutlet var imgCell: UIImageView!

@IBOutlet var labelCell: UILabel!

}

推荐答案

您的错误很容易解释.在设置 collectionView.dataSource 的位置上,您有一个错误源-设置了 UIView 实例的不是实现 UICollectionViewDataSource 的东西.

Your error is self-explanative enough. In place, where you set collectionView.dataSource you have an error source - instead of something implementing UICollectionViewDataSource you set UIView instance.

我认为,您需要类似的东西

I think, you need something like

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    collectionView.dataSource = self;
 }

这篇关于将CollectionView连接到ViewController,连接dataSource-Swift-XCode时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆