2015-01-07 40 views
0

我有一个包含图像视图和标签的CollectionView单元。当我尝试设置任何图像或我的程序崩溃,这个误差范围内的标签中的文字:尝试访问已注册单元的成员时出现运行时错误

fatal error: unexpectedly found nil while unwrapping an Optional value 

细胞是从UICollectionViewCell继承自定义类,以及出口已创建和链接故事板:

class CustomCell: UICollectionViewCell { 

    @IBOutlet var thumbnailImageView: UIImageView! 
    @IBOutlet var textLabel: UILabel! 
} 

我设置ImageView的有:

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

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CustomCell 
     cell.thumbnailImageView.image = UIImage(named: "ss") 
     return cell 
    } 

我本来以为有什么不对的图像文件的位置,所以我评论它,并试图改变标签的文字:

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

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CustomCell 
     cell.textLabel.text = "Hello" 
     return cell 
    } 

但这也导致了崩溃。什么会导致自定义单元格正确加载,但无法访问其任何成员?

编辑:在viewDidLoad中功能我注册了我的课是这样的:

self.collectionView?.registerClass(CustomCell.self, forCellWithReuseIdentifier: reuseIdentifier) 

编辑: enter image description here

+0

你连接的网点? –

+0

是的,我把它们中的每一个都连接到故事板 –

+0

单元格是否为零?你检查了吗? –

回答

1

,如果电池是在故事板制作你不应该注册类。删除该行。确保您为故事板中的单元格设置标识符。如果您完全使用代码完成单元,则只注册该类。

+0

完成并完成。感谢堆 –

相关问题