【问题标题】:Crash when loading UICollectionView in, why?加载 UICollectionView 时崩溃,为什么?
【发布时间】:2014-08-06 12:16:49
【问题描述】:

没有错误日志,仅此而已。

我有一个动态原型 UICollectionViewCell,并在这里加载:

override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {

    var cell: LeftMenuCollectionViewCell

cell = collectionView.dequeueReusableCellWithReuseIdentifier("xxx", forIndexPath: indexPath) as LeftMenuCollectionViewCell // <- at this line something goes wrong

    return cell
}

我之前正在注册课程:

self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "xxx")

现在使用 iOS 8 beta 5

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell uicollectionreusableview


    【解决方案1】:

    您注册了UICollectionViewCell,而不是您自己的单元类LeftMenuCollectionViewCell。让

    UICollectionviewCell as LeftMenuCollectionViewCell 
    

    正在破坏并导致崩溃。要修复此注册正确的类

    self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "xxx")
    

    【讨论】:

      【解决方案2】:

      同意下面的两个解决方案,我注册单元格错误,但后来我发现了另一个问题,cell's outlets become nil。如果 UICollectionViewController 是通过 Interface Builder 创建的,则解决方案是删除整个注册

      【讨论】:

        【解决方案3】:

        您错误地注册了单元格。试试这个:

        self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "xxx")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多