János
János

Reputation: 35050

Why is UICollectionViewCell's outlet nil?

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, tha label has a nil value.

override func viewDidLoad() {
    super.viewDidLoad()

    // Register cell classes
    self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
}

override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {

    var cell: LeftMenuCollectionViewCell
    cell = collectionView.dequeueReusableCellWithReuseIdentifier("ls", forIndexPath: indexPath) as LeftMenuCollectionViewCell
    println(cell.label) // <- this is nil, why??
    cell.label.text = "asd"

    return cell
}

And the subclassed cell:

class LeftMenuCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
}

Upvotes: 156

Views: 42638

Answers (8)

Milosh Milivojevich
Milosh Milivojevich

Reputation: 19

You didn't register your cell,

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

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

Upvotes: -1

dgalluccio
dgalluccio

Reputation: 83

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")

Upvotes: -1

Sergio
Sergio

Reputation: 2473

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")

Upvotes: 15

Vineeth
Vineeth

Reputation: 1750

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")

Upvotes: 58

Michael
Michael

Reputation: 9864

Gotta register that nib guys!

collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "CustomCellId")

Upvotes: 17

能蟹仔
能蟹仔

Reputation: 767

Just remove this line:

self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")

Upvotes: 58

justRadojko
justRadojko

Reputation: 257

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

Upvotes: -1

J&#225;nos
J&#225;nos

Reputation: 35050

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.

Upvotes: 338

Related Questions