6

Please consider the following code:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("DataItemCell", forIndexPath: indexPath) as DataItemCollectionViewCell

    println("\(cell.parametersView.subviews.count)")

    return cell
}

and the view hierarchy of the cell:

view hierarchy

I can't figure out why output is 0.

What I'm trying to achieve is to recursively loop through all descendants of parametersView.

Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162

3 Answers3

4

I try to simulate a similar project and I had the same issue (my prototype cell wasn't used). Be sure that you don't have any

    self.collectionView.registerClass(UICollectionViewCell, forCellWithReuseIdentifier: reuseIdentifier)

or

    self.collectionView.registerClass(DataItemCollectionViewCell, forCellWithReuseIdentifier: "DataItemCell"DataItemCollectionViewCell)

anywhere in your code.

Usually, there is one in the func viewDidLoad() function.

Edit:

Send me an exemple by mail, if you still have the problem.

Florian
  • 855
  • 7
  • 12
2

Well I did my own collection view and tried to replicate your problem and what I noticed it's that subviews is ignoring all the UIlabels, but if you add, for example a UIImageView, the array subviews will have it, this question had a similar problem but the solution they provided there didn't work for me.
As a workaround you could link your labels as IBOutlets to your cell prototype DataItemCollectionViewCell, I know it's far from clean, but if you're really desperate you can try that.
On a side note, I'm using Xcode Version 6.1 (6A1052d), so maybe they changed something in UILabel in this new release.

Community
  • 1
  • 1
aldoram5
  • 1,556
  • 3
  • 18
  • 18
1

It looks like your parameters view is being added as a subview of your cell but the parametersView property of the cell is not being set. Make sure your outlet is hooked up in the storyboard/xib.

I bet if you print out cell.parametersView you'll get nil.

Lance
  • 8,872
  • 2
  • 36
  • 47