Why is UICollectionViewCell's outlet nil?

后端 未结 8 2094
情话喂你
情话喂你 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:43

    If you are using xib, make sure that you have added this line of code to your viewdidload.

    Objective C:

    [self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"MyCellIdentifier"];
    

    Swift:

    collectionView.register(UINib(nibName:"MyCell", bundle: nil), forCellWithReuseIdentifier:"MyCellIdentifier")
    
    0 讨论(0)
  • 2020-11-29 17:46

    Looks like there's two ways to register and I was using the wrong one the first. I have a custom xib view so registered with the second option, and we have data!

    1:

    collectionView?.register(YourItemClassName.self, forCellWithReuseIdentifier: "yourIdentifier") 
    

    2:

    collectionView?.register(UINib(nibName: "YourItemClassName", bundle: nil), forCellWithReuseIdentifier: "yourIdentifier")
    
    0 讨论(0)
提交回复
热议问题