【问题标题】:Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:]-[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] 中的断言失败
【发布时间】: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 中。之后它工作正常

标签: ios swift


【解决方案1】:

正如帖子中提到的,我在 tablecell xib 文件中工作并尝试按照我的需要做,但是一旦我改变我的方法,我在情节提要内的 UITableView 中创建 tableview 单元格并更新所有插座然后运行应用程序。该应用程序运行良好....集合视图单元格中的所有出口都是通过引用其静态变量动态创建的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多