在 – 断言失败

我正在创build一个表视图和一个自定义UItableviewCell我正在使用集合视图,我也创build一个自定义collectionview单元格。 在TableViewCell中设置集合视图的所有基本function之后,运行应用程序时,我得到了崩溃的原因:

没有通过调用-dequeueReusableCellWithReuseIdentifier:forIndexPath:或者是nil(>)来检索由于未捕获的exception'NSInternalInconsistencyException'导致的终止应用程序,原因:'从-collectionView:cellForItemAtIndexPath:({length = 2,path = 0 – 0} )”

我试图寻找更多,但无法find任何方向

这里是我的代码片段:1.在TableViewcell中awakeFrom Nib:

override func awakeFromNib() { super.awakeFromNib() // self.collectionView_Files.registerNib(UINib(nibName: "MediaCollectionCell", bundle: nil), forCellWithReuseIdentifier: "MediaCollectionCell") self.collectionView_Files.registerClass(MediaCollectionCell.self, forCellWithReuseIdentifier: "MediaCollectionCell") } 

CollectionView方法:

 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return arrFolderData.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let simpleTableIdentifier = "MediaCollectionCell" var cell: MediaCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier(simpleTableIdentifier, forIndexPath: indexPath) as! MediaCollectionCell cell = NSBundle.mainBundle().loadNibNamed(simpleTableIdentifier, owner: self, options: nil)[0] as! (MediaCollectionCell) let dict = arrFolderData[indexPath.row] if(dict["file_type"] as! String == "0") { /// Means image cell.imgView_Item.image = UIImage(named: "images_41") cell.btn_ViewImage.hidden = false cell.btn_PlayVideo.hidden = true } else if (dict["file_type"] as! String == "1") { /// Means video cell.btn_ViewImage.hidden = true cell.btn_PlayVideo.hidden = false cell.imgView_Item.image = UIImage(named: "video_thumb_icon") } // cell.backgroundColor = arrFolderData[indexPath.item] return cell } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("Collection view at row \(collectionView.tag) selected index path \(indexPath)") } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { let length = (UIScreen.mainScreen().bounds.width-15)/2 return CGSizeMake(length,length); } 

正如在后文中提到的,我在tablecell xib文件中工作,并尝试按照我的需要做,但是一旦我改变了我的方法,我创buildtableview单元格到故事板内的UITableView并更新所有sockets,然后运行应用程序。 该应用程序工作正常….集合视图单元格中的所有sockets是dynamic创build的,并从其静态variables中引用。