0

I'm trying to implement a very basic collection view with a cell subclass that's been designed in IB. A screenshot may better explain the issue that I'm seeing, but basically the image view I've added to my cell in IB does not show up as a subview inside the cell, but a subview of the collection view. Additionally, the outlet in my cell subclass is not hooked up.

As you can see in the screenshot, on the right, I've added the ImageView to the cell, and the view hierarchy display in IB shows the imageview as a subview of the cell (technically the cell's content view, but I don't think IB differentiates that here). However, on the left (where I've gone to Debug->View Debugging->Capture View Hierarchy), you can see the image views are skewed along one dimension, and are not part of the cell's contentview's subviews. I have followed Ray Wenderlich's tutorial, and have implemented CollectionViews before (although I'm definitely no expert), but I can't figure out what is different about my project. And I'm not sure how to debug what's going inside the cell, because it doesn't implement traditional view methods like viewDidLayoutSubviews, etc. Any tips on something probably silly I'm doing are much appreciated!

Screenshot of XCode & Storyboard

neal
  • 557
  • 2
  • 9

2 Answers2

4

Thanks again for your help and time spent. The problem was that I was designing the cell in IB but also calling

self.collectionView!.registerClass(TestCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 

inside the collection view controller viewDidLoad method (actually the template code for a UICollectionViewController subclass does this automatically). When I did that, the cell that is instantiated based on the reuse identifier is not related to the one in the story board, so the outlets aren't hooked up

neal
  • 557
  • 2
  • 9
  • Thanks! I think a lot of people will run into this since Xcode's template code for `UICollectionViewController` has a call like this. So if you don't notice it and you try to use prototype cells in your storyboard, you'll be sad. – Mike Greiner May 05 '22 at 14:45
0

In your case, i think you have not correctly setup your Viewcontroller/collection view controller class with IB Outlet. Please follow below step to create cell in collection view with custom object(Image view, label etc.).

1) First of all provide your custom Class file according to your IB UI. 
Please review attached sc.

enter image description here

Here I have first of all create class file(.h & .m). For Ex:

//  CollectionViewController.h
#import <UIKit/UIKit.h>

@interface CollectionViewController : UICollectionViewController
@property (weak, nonatomic) IBOutlet UIImageView *CollectionIV;

@end

2) Take Collectionview controller IB UI in storyboard or .xib.
3) Connect your class file with IB UI. Review above SC.
4) Optional - If required then setup correct DataSource and Delegate method with UI.
Renish Dadhaniya
  • 10,642
  • 2
  • 31
  • 56
  • thanks for the detailed response & screenshots, but all of this is configured correctly! – neal Dec 08 '15 at 02:02
  • @neal, can you sent demo of your project? so, i will try to figure out. – Renish Dadhaniya Dec 08 '15 at 04:40
  • Hi Renish,Thanks again for your help and time spent. The problem was that I was designing the cell in IB but also calling self.collectionView!.registerClass(TestCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) inside the collection view controller viewDidLoad method (actually the template code for a UICollectionViewController subclass does this automatically). I guess when I do that, the cell that is instantiated based on the reuse identifier is not related to the one in the story board, so the outlets aren't hooked up. – neal Dec 12 '15 at 20:05