CollectionView: cellForItemAtIndexPath: why does my view have null objects?

Pay attention to 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 cell view hierarchy:

view hierarchy

I can not understand why the output is 0 .

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

+6
source share
3 answers

I am trying to simulate a similar project and I had the same problem (my prototype cell was not used). Make sure you don’t have

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

or

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

anywhere in your code.

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

Edit:

Email me an example if you still have a problem.

+4
source

Well, I made my own presentation of the collection and tried to replicate your problem, and I noticed that subviews ignores all UIlabels , but if you add, for example, UIImageView, then in the subviews of this array there will be this question had a similar problem, but the solution they they were provided, did not work for me.
As a workaround, you could associate your labels as an IBOutlet with the prototype of your DataItemCollectionViewCell cell, I know this is far from clean, but if you are really desperate, you can try this.
On the other hand, I am using Xcode Version 6.1 (6A1052d), so maybe they have changed something in UILabel in this new version.

+2
source

It looks like the representation of your parameters is added as a sub-supply to your cell, but the cell's parametersView property is not set. Make sure your outlet is plugged in / xib storyboard.

I bet if you print out cell.parametersView you get zero.

+1
source

Source: https://habr.com/ru/post/978334/


All Articles