我有一个collectionViewController,我想显示一些自定义UICollectionViewCell与他们上的一些标签.不幸的是,每当我尝试访问自定义UICollectionViewCell的标签,它会导致崩溃:
@H_403_28@安慰
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) } }