在我的项目中,我在UICollectionViewCell中创建了Cell
由于未捕获的异常,其出现了“错误终止”应用程序
代码如下。
GalleryCell.swift
class GalleryCell: UICollectionViewCell { @IBOutlet var titleLabel : UILabel init(coder aDecoder: NSCoder!) { super.init(coder: aDecoder) } }
我在My ViewController中使用了该单元格:
代码如下:
NextViewController.swift
import UIKit class NextViewController: UIViewController { @IBOutlet var collectionView : UICollectionView var ListArray=NSMutableArray() override func viewDidLoad() { super.viewDidLoad() for i in 0..70 { ListArray .addObject("C: \(i)") } } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section:Int)->Int { return ListArray.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell { var cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as GalleryCell cell.titleLabel.text="\(ListArray.objectAtIndex(indexPath.item))" return cell } func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize { return CGSizeMake(66, 58) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
}
我的问题是我遇到以下错误:
***** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CELL - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'*** First throw call stack:**
我加入以下两行NextViewController.swift下viewDidLoad():
viewDidLoad()
var nibName = UINib(nibName: "GalleryCell", bundle:nil) collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CELL")
问题是我没有注册笔尖。现在我正在这样做,它工作正常。