我正在創建一個表視圖和自定義UItableviewCell內我正在使用集合視圖,我也創建一個自定義collectionview單元格。設定TableViewCell集合視圖中的所有基本功能後,當我跑了有理性崩潰的應用程序:聲明失敗 - [UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:]
終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,理由是:「認爲從-collectionView返回:cellForItemAtIndexPath :({長度= 2,路徑= 0 - 0})沒有通過調用-dequeueReusableCellWithReuseIdentifier檢索到:forIndexPath:或爲nil(>)」
我試圖尋找更多的錢,但無法找到任何方向
這是我的代碼片段: 1.在TableViewcell awakeFrom筆尖:
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);
}
刪除此行:'cell = NSBundle.mainBundle()。loadNibNamed(simpleTableIdentifier,owner:self,options:nil)[0] as! (MediaCollectionCell)' – sage444
感謝您的評論,但我使用自定義收集單元格....現在我刪除了該行....這次沒有崩潰,但沒有數據顯示在單元格中,沒有單元格顯示.....你能否建議我另一種方法....? –
對於這次崩潰,我改變了tableCell和collectionCell。我刪除了兩個單元格的xib,並將這些單元格添加到故事板tableView中。之後,它工作正常 –