How to create UICollectionViewCell programmatically

前端 未结 5 589
予麋鹿
予麋鹿 2020-12-23 20:21

I\'m trying to create UICollectionView programatically. I need to add labels inside the cells, so I Created CollectionViewCell class.

This

5条回答
  •  太阳男子
    2020-12-23 20:47

    Your problem lies here. In your viewDidLoad(), you're registering your collectionView cell twice. You are registering the collectionview's cell to your custom cell class in the first line and then in the second line you are registering it to the class UICollectionViewCell.

     collectionView.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
     collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
    

    Just remove the second line and your code should work.

提交回复
热议问题