除了从适当的 类 继承外,还需要采取哪些额外步骤才能在 Xcode 中创建自定义 table 单元格?

Other than inheriting from the appropriate classes, what additional steps need to be taken to create a custom table cell in Xcode?

更具体地说,我创建了一个 CollectionView,其中包含一个 CollectionViewCell。在我的 CollectionViewController 代码中是:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

为我完成整个出列单元的功能。我已经跟踪了我的程序,它在这一行崩溃了(它将我发送到汇编代码):

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CustomCell

但是,当我将 "CustomCell" 更改为 "UICollectionViewCell" 时,它运行良好。这对我来说很奇怪,因为我的 CustomCell class 已经采用了 UICollectionViewCell 的属性,如以下行所示:

import UIKit

class CustomCell: UICollectionViewCell {

    @IBOutlet var thumbnailImageView: UIImageView!
}

我还在这个视图的初始化和故事板中设置了 reuseIdentifier。我的问题是:为 table/collection 视图创建自定义单元格时,需要采取哪些额外步骤?

编辑:这是具体的崩溃屏幕:

您需要将 Nib 或 Class 注册为重复使用标识符的单元格。在代码中使用这些方法:

func registerClass(_ cellClass: AnyClass!, forCellWithReuseIdentifier identifier: String!)
func registerNib(_ nib: UINib!, forCellWithReuseIdentifier identifier: String!)

崩溃是由于 as 运算符返回一个可选的。它被强制展开并崩溃,因为标识符没有为您的 class 注册,因此单元格是 UICollectionViewCell,而不是您的自定义 class.

参见: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-ID338