【发布时间】:2017-01-13 19:44:38
【问题描述】:
我正在创建一个表格视图,并在自定义 UItableviewCell 中使用集合视图,并且我还创建了一个自定义集合视图单元格。在 TableViewCell 中设置集合视图的所有基本功能后,运行应用程序时出现崩溃,原因是:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“从 -collectionView:cellForItemAtIndexPath: ({length = 2, path = 0 - 0}) 返回的视图未通过调用 -dequeueReusableCellWithReuseIdentifier:forIndexPath: 或被检索到无(>)'
我试图搜索更多但找不到任何方向
这是我的代码 sn-p: 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);
}
【问题讨论】:
-
删除这一行:
cell = NSBundle.mainBundle().loadNibNamed(simpleTableIdentifier, owner: self, options: nil)[0] as! (MediaCollectionCell) -
感谢您的评论,但是我使用自定义集合单元格......现在我删除了该行......这次没有崩溃,但单元格中没有显示数据,没有单元格显示...... ....你能建议我另一种方法吗....?
-
对于这次崩溃,我更改了 tableCell 和 collectionCell。我删除了两个单元格的 xib 并将这些单元格添加到故事板 tableView 中。之后它工作正常