Why is UICollectionViewCell's outlet nil?

后端 未结 8 2093
情话喂你
情话喂你 2020-11-29 16:56

I have created a custom UICollectionViewCell in Interface Builder, binded views on it to the class, and then when I want to use and set a string to the label on the string,

相关标签:
8条回答
  • 2020-11-29 17:24

    I am calling self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls") again. If you are using a storyboard you don't want to call this. It will overwrite what you have in your storyboard.

    If you still have the problem check wether reuseIdentifier is same in dequeueReusableCellWithReuseIdentifier and in storyboard.

    0 讨论(0)
  • 2020-11-29 17:24

    Just remove this line:

    self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
    
    0 讨论(0)
  • 2020-11-29 17:24

    Gotta register that nib guys!

    collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "CustomCellId")
    
    0 讨论(0)
  • 2020-11-29 17:34

    You didn't register your cell,

    fileprivate let yourIdentifier = ""yourIdentifier"
    super.viewDidLoad() { 
    

    //here you need to register cell collectionView?.register(NameOfYourClass.self, forCellWithReuseIdentifier: "yourIdentifier") }

    0 讨论(0)
  • 2020-11-29 17:35

    I think that best solution is to directly use from storyboard where add a CollectionView, in alternative you need to remove a CollectionViewCell from your CollectionView in storyboard and register a cell with the following command:

    collectionView?.register(UINib(nibName: "YourItemClassName", bundle: nil), forCellWithReuseIdentifier: "yourIdentifier")

    0 讨论(0)
  • 2020-11-29 17:38

    I had a similar problem, but my mistake was that I didn't delegate CollectionViewCell to be able to change the label text..

    0 讨论(0)
提交回复
热议问题