[英]iOS Swift - Custom UICollectionViewCell in Custom UITableViewCell
我已將集合視圖添加到自定義表格視圖單元格中,現在嘗試在其中添加自定義集合視圖單元格。 我沒有運氣注冊筆尖並將單元格加載到cellForItemAtIndexPath中。
我一直收到此錯誤:
2015-01-08 17:51:23.760 puma[9018:533346] *** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UICollectionView.m:3318
2015-01-08 17:51:23.764 puma[9018:533346] *** 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'
相關代碼:
class CustomTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? CustomCollectionViewCell
if (cell == nil) {
let nib = NSBundle.mainBundle().loadNibNamed("CustomCollectionCell", owner: CustomCollectionViewCell.self, options: nil) as NSArray
cell = nib.objectAtIndex(0) as? CustomCollectionViewCell
}
// Configure the cell
return cell!
}
}
class CustomCollectionViewCell: UICollectionViewCell {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
由於某種原因,這最近突然出現了很多。 將以下代碼添加到表視圖控制器上的viewDidLoad中:
if let myTableView = self.tableView {
self.tableView!.registerNib(UINib(nibName: "NibName", bundle: nil), forCellReuseIdentifier: "Cell")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.