swift_dynamicCastClassUnconditional运行时错误

我迅速遇到了一个奇怪的运行时错误。我有以下顺序的CollectionViewController场景:

Items Controller Scene
    Items Controller (class: ItemsController)
        ItemCell (class: ItemCell, reuseIdentifier: "ItemCell")
            View...

和类:

class ItemCell : UICollectionViewCell {

    @IBOutlet weak var nameLabel: UILabel!

}

class ItemsController : UICollectionViewController {

        override func viewDidLoad() {
            super.viewDidLoad()

            self.collectionView.registerClass(
                UICollectionViewCell.self,
                forCellWithReuseIdentifier: "ItemCell");

        }

        override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
            return 1
        }


        override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 10
        }

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

            let cell = collectionView.dequeueReusableCellWithReuseIdentifier(               "ItemCell",
            forIndexPath: indexPath) as ItemCell

            // ^^ The runtime error points to here

            cell.nameLabel.text = "Title";

            return cell
        }

}

这没有任何意义。我究竟做错了什么?我曾尝试清理项目并重新编译,但仍然无法正常工作(这通常适用于大多数情况)。我不想重写所有可以正常工作的东西(这也发生在我身上;重新创建视图可以解决此问题)。我使用的是Xcode 6.1;如果这里没有错误,我将继续创建故事板,但在确定代码没有问题之前,我不想这样做。

编辑:忘记提及它。如果我将类从ItemCell更改为UICollectionViewCell collectionView.dequeueReusableCellWithReuseIdentifier,它将至少运行。因此,问题确实存在于用于指定自定义类名称的情节提要中。

没关系,发现了问题。在为收集视图注册类时,我忘记将类名称更改为ItemCell:

self.collectionView.registerClass(ItemCell.self, forCellWithReuseIdentifier: "ItemCell")

编辑:我对UICollectionViewController并没有太多的经验,但是我发现,如果我想使用上述行使用自定义类,则会使所有IBOutlets为零。因此,删除该行可解决此问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章