我收到这个错误,试图在 Swift 中使用 UICollectionView:
NSInternalInconsistencyException', reason: 'attempt to register a cell class which is not a subclass of UICollectionViewCell ((null))
但我想我正在注册单元格:
ViewDidLoad:
override func viewDidLoad() { super.viewDidLoad() self.collectionView.registerClass(NSClassFromString("CollectionCell"),forCellWithReuseIdentifier:"CELL") }
cellForItemAtIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell { var cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as CollectionCell cell.titleLabel.text="cellText" return cell }
和细胞类:
class CollectionCell: UICollectionViewCell
{
@IBOutlet var titleLabel : UILabel
init(coder aDecoder: NSCoder!)
{
super.init(coder: aDecoder)
}
}
感谢任何帮助
最佳答案
您需要以 Swift 样式将 UICollectionViewCell 的子类传递给 registerClass:
self.collectionView.registerClass(CollectionCell.self, forCellWithReuseIdentifier:"CELL")
关于ios - 使用 Swift 的 UICollectionView 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24179236/