iOS Swift - 自定义 UITableViewCell 中的自定义 UICollectionViewCell
iOS Swift - Custom UICollectionViewCell in Custom UITableViewCell
我已将集合视图添加到自定义 table 视图单元格,现在我正尝试在其中添加自定义集合视图单元格。我没有运气注册笔尖并将单元格加载到 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
}
}
出于某种原因,这最近突然出现了很多。将以下代码添加到 table 视图控制器上的 viewDidLoad:
if let myTableView = self.tableView {
self.tableView!.registerNib(UINib(nibName: "NibName", bundle: nil), forCellReuseIdentifier: "Cell")
}
我已将集合视图添加到自定义 table 视图单元格,现在我正尝试在其中添加自定义集合视图单元格。我没有运气注册笔尖并将单元格加载到 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
}
}
出于某种原因,这最近突然出现了很多。将以下代码添加到 table 视图控制器上的 viewDidLoad:
if let myTableView = self.tableView {
self.tableView!.registerNib(UINib(nibName: "NibName", bundle: nil), forCellReuseIdentifier: "Cell")
}