Could not cast value of type 'UICollectionViewCell'

后端 未结 6 1104
野性不改
野性不改 2021-02-01 12:21

I use a custom CollectionViewCell in my Storyboard. When I start the app I get this message:

Could not cast value of type \'UICollectionViewCell\' to T

6条回答
  •  北海茫月
    2021-02-01 12:57

    The template for a CollectionViewController in Xcode comes with this line of code in viewDidLoad, which changes the specification in your storyboard.

    // Register cell classes
    self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    

    Simply delete that line.

    In older Swift versions the line is:

    // Register cell classes
    self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    

提交回复
热议问题