1

This label seems to return nil, even though I have the reuseIdentifier and tag set properly.

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var identifier: String = "CollectionCell"
    var cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! UICollectionViewCell

    // Configure the cell


    //save till later, when images are actually present

    //var cellItem1 = hostManager[indexPath.row * 2]

    let label:UILabel = cell.viewWithTag(1) as! UILabel
    return cell

}

The program breaks where the label is set = to the viewWithTag. I have no custom class set for the cell, just the prototype. The tag is set on the storyboard. Getting an error "EXC_BAD_INSTRUCTION...". Any help would be appreciated, Thanks!

  • Just do this for debugging :- http://stackoverflow.com/a/21399753/2714702 ... and check whether you are getting the view tag in the sub-views. according to that you should proceed. – Vizllx Aug 03 '15 at 07:51

2 Answers2

6

Try removing this line from viewDidLoad:

self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
wogsland
  • 9,106
  • 19
  • 57
  • 93
Carlos Borges
  • 81
  • 1
  • 4
1

I have just created a sample project with your code and for me it works. Although you shouldn't force unwrap.

Make sure you have the correct setup in your storyboard:

Check if your collectionViewCell is setup correctly:

And set the tag of your label:

Here is the sample project

Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
  • your sample project works fine for me. I recreated a VC and put a CollectionView in it, hooked it up to delegate and datasource, set my tag on the label and few other buttons I have in the cell, however, I still get the error – NotReallyButOkay Aug 04 '15 at 03:01