gpt4 book ai didi

ios - 使用 Swift 的 UICollectionView 错误

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:25 24 4
gpt4 key购买 nike

我收到这个错误,试图在 Swift 中使用 UICollectionView:

NSInternalInconsistencyException', reason: 'attempt to register a cell class which is not a subclass of UICollectionViewCell ((null))

但我想我正在注册单元格:

  1. ViewDidLoad:

    override func viewDidLoad()
    {
    super.viewDidLoad()
    self.collectionView.registerClass(NSClassFromString("CollectionCell"),forCellWithReuseIdentifier:"CELL")
    }
  2. 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com