neal
neal

Reputation: 557

UICollectionViewCell subclass outlet not connected

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

Upvotes: 0

Views: 873

Answers (2)

neal
neal

Reputation: 557

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

Upvotes: 4

Renish Dadhaniya
Renish Dadhaniya

Reputation: 10752

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.

Upvotes: 0

Related Questions