【发布时间】:2016-03-02 20:49:37
【问题描述】:
我有一个非常简单的 swift 程序,它使用 UICollectionView 中的 Nib 构建的自定义单元格。
当我运行应用程序时,我收到一条错误提示
:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在包中加载 NIB:”
我查看了所有教程并完全按照它们进行操作,看起来我以某种方式缺少初始化程序,但我不知所措。在故事板编辑器中 CollectionViewController 的主要默认单元格中,我将重用标识符命名为“Cell”,对于 Nib(swift 文件名为 CustomCollectionViewCell.xib),我将重用标识符命名为“CustomCell”。我在 Xib 中有一个 UIButton 作为一个动作,如果按下它应该记录成功。
这里是mainVC:
class MainCollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "Cell")
// Do any additional setup after loading the view.
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 1
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCollectionViewCell
// Configure the cell
return cell
}
}
这里是 CustomCollectionViewCell 代码:
class CustomCollectionViewCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBAction func testButton(sender: UIButton) {
print("Success!")
}
}
我知道这是一个基本的初学者项目,但我在网络上到处查看并且很难过。任何帮助将不胜感激。
【问题讨论】:
-
我忘了添加我确实在上面的主 VC 中定义了 let reuseIdentifier = "Cell"。
标签: ios swift uicollectionview nib