inheritanceUICollectionViewCell并从xib初始化

我正在注册这样的单元格:

collectionView!.registerNib(UINib(nibName: "ItemCell", bundle: nil), forCellWithReuseIdentifier: "ItemCellIdentifier") 

并像这样访问它:

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

ItemCell是UICollectionViewCell的子类:

 class ItemCell: UICollectionViewCell { @IBOutlet weak var itemImage: UIImageView! @IBOutlet weak var itemImageDescription: UILabel! //creation from nib requires this method to be overridden override func awakeFromNib() { super.awakeFromNib() // Initialization code } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } } 

最后,我有一个带有类ItemCell的UICollectionViewCell的xib。

我不断收到错误:

在 – [UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:],/ SourceCache /UIKit/UIKit-3347.44.2/UICollectionView.m:3443断言失败。

崩溃在这一行上:

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

编辑

 collectionView!.registerNib(UINib(nibName: "ItemCell", bundle: nil), forCellWithReuseIdentifier: "ItemCellIdentifier") 

被称为

 override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) } 

包含在:

 class CollectionCell : UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate { 

你在哪里打电话registerNib

我再现了这个设置,以匹配我可以告诉你的。

这适用于我,没有错误:

 import UIKit class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { var model:[String] = [] @IBOutlet weak var collectionView: UICollectionView! override func viewDidLoad() { super.viewDidLoad() // just a simple test model ... for index in 1...100 { model.append("\(index)") } // register the nib collectionView.registerNib(UINib(nibName: "ItemCell", bundle: nil), forCellWithReuseIdentifier: "ItemCell") // this is just a test layout var layout = UICollectionViewFlowLayout() layout.itemSize = CGSize(width:200.0,height:200.0) layout.scrollDirection = UICollectionViewScrollDirection.Vertical self.collectionView.collectionViewLayout = layout self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.reloadData() } // MARK: - UICollectionViewDataSource func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ return model.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ var cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("ItemCell", forIndexPath: indexPath) as! UICollectionViewCell return cell } } 

尝试在主包中search

 collectionView!.registerNib(UINib(nibName: "ItemCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "ItemCellIdentifier") 

崩溃是因为向下转换失败了。 而不是使用强制forms! 使用有条件的?

试试这种方式来注册单元格:

viewDidLoad中

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

cellForItemAtIndexPath

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell { var cell = collectionView.dequeueReusableCellWithReuseIdentifier("ItemCellIdentifier", forIndexPath: indexPath) as ItemCell cell.titleLabel.text="cellText" return cell }