我在swift中遇到了一个奇怪的运行时错误。我按以下顺序有一个CollectionViewController场景:
Items Controller Scene
Items Controller (class: ItemsController)
ItemCell (class: ItemCell, reuseIdentifier: "ItemCell")
View...
班级:
class ItemCell : UICollectionViewCell {
@IBOutlet weak var nameLabel: UILabel!
}
class ItemsController : UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.registerClass(
UICollectionViewCell.self,
forCellWithReuseIdentifier: "ItemCell");
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier( "ItemCell",
forIndexPath: indexPath) as ItemCell
// ^^ The runtime error points to here
cell.nameLabel.text = "Title";
return cell
}
}
这没有任何意义。我究竟做错了什么?我已经尝试清理项目并重新编译但仍然无法工作(这通常适用于大多数情况)。我不想重写一切工作(这也发生在我身上;重新创建视图解决了问题)。我在Xcode 6.1上;如果这里没有错误,我将继续重新创建我的故事板但我不想这样做,直到我确定我的代码没有问题。
编辑:忘记提及它。如果我将类从ItemCell更改为collectionView.dequeueReusableCellWithReuseIdentifier
行中的UICollectionViewCell,它至少会运行。因此,问题确实存在于故事板中,用于分配自定义类名。
答案 0 :(得分:0)
没关系,发现了问题。在注册集合视图的类时,我忘了将类名更改为ItemCell:
self.collectionView.registerClass(ItemCell.self, forCellWithReuseIdentifier: "ItemCell")
编辑:我对UICollectionViewController没有多少经验,但我发现,如果我想使用上面的行使用自定义类将使所有IBOutlets为零。因此删除该行可以解决问题。