我正在注册这样的单元格:
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)
}
}
最后,我的xib带有一个带有ItemCell类的UICollectionViewCell。
我一直收到错误:
断言失败 - [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 {
答案 0 :(得分:1)
尝试搜索主要包
collectionView!.registerNib(UINib(nibName: "ItemCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "ItemCellIdentifier")
答案 1 :(得分:1)
你在哪里打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
}
}
答案 2 :(得分:0)
崩溃是因为向下转型失败了。而不是使用强制形式!使用条件一个?
尝试另一种方法来注册细胞:
<强> 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
}