标签出口在自定义UICollectionViewCell在Swift导致可选.没有崩溃

我有一个collectionViewController,我想显示一些自定义UICollectionViewCell与他们上的一些标签.不幸的是,每当我尝试访问自定义UICollectionViewCell的标签,它会导致崩溃:

安慰

fatal error: Can’t unwrap Optional.None

窗口

Thread1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)

我正在尝试访问标签:

cell.name.text = names[indexPath!.item]

也许这是从我的出口标签是零?但是看看周围的答案没有任何工作,因为我不确定什么问题添加?/!在我的代码中并没有真正的帮助.

MyCustomUICollectionViewController

class ScrambledTextCollectionViewController: UICollectionViewController {

    var names: String[] = ["Anna","Alex","Brian","Jack"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView.registerClass(MyCustomCollectionViewCell.self,forCellWithReuseIdentifier: reuseIdentifier)

    }

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

    override func collectionView(collectionView: UICollectionView?,numberOfItemsInSection section: Int) -> Int {
        return names.count
    }

    override func collectionView(collectionView: UICollectionView?,cellForItemAtIndexPath indexPath: NSIndexPath?) -> UICollectionViewCell? {
        var cell = collectionView?.dequeueReusableCellWithReuseIdentifier("Cell",forIndexPath: indexPath) as MyCustomCollectionViewCell

        cell.name.text = names[indexPath!.item]

        return cell
    }
}

MyCustomCollectionViewCell

class MyCustomCollectionViewCell: UICollectionViewCell {

    @IBOutlet var name: UILabel
    init(frame: CGRect) {
        super.init(frame: frame)                
    }
}
找到答案 here

Remove,self.collectionView.registerClass(MyCustomCollectionViewCell.self,forCellWithReuseIdentifier:reuseIdentifier)

阅读链接的详细原因为什么

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...