Josué H.
Josué H.

Reputation: 1191

Error when set UICollectionViewController - nib but didn't get a UICollectionView

I have a trouble when set custom cell into UICollectionViewController.

This is my code.

KidsDetailViewController.h

 #import <UIKit/UIKit.h>

 @interface KidsDetailViewController : UICollectionViewController <UICollectionViewDataSource>
 @property NSNumber *idCampana;

@property (weak, nonatomic) IBOutlet UICollectionView *grid;

@end

KidsDetailViewController.m

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

  return [grid_kid count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

GridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:self.truckImages[0]];

cell.prd_img.image = [UIImage imageNamed:@"ic_launcher_58x58"];

return cell;
}

GridCell.h

#import <UIKit/UIKit.h>

 @interface GridCell : UICollectionViewCell
 @property (weak, nonatomic) IBOutlet UIImageView *prd_img;

@end

GridCel.m

 #import "GridCell.h"

 @implementation GridCell

 - (instancetype)initWithFrame:(CGRect)frameRect {
      self = [super initWithFrame:frameRect];
      if (self) {
          // Initialization code
      }
   return self;
 }

 @end

This is the Error.

   *** Assertion failure in -[KidsDetailViewController loadView], /SourceCache/UIKit_Sim/UIKit-3283.1/UICollectionViewController.m:166
   2014-07-28 02:25:18.874 Geelbe[7673:231598] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "jXd-Ir-mr4-view-0IK-5Q-NzC" nib but didn't get a UICollectionView.'
  *** First throw call stack:

I've tried everything and still gives error.

Thanks.

Upvotes: 4

Views: 5071

Answers (4)

lazarus
lazarus

Reputation: 21

How to setup a collection view controller with a nib (.xib) file.

Add a new file by selecting the "Empty" template from the "User Interface" section. Drag a UICollectionView object from the object library over.

1) Associate the nib which collection view controller class.

In the Document Outline (left hand section), under "Placeholders", select "File's Owner". Show the Identity Inspector and set the class dropdown to your subclass of UICollectionViewController.

2) Associate the nib with the UICollectionView.

Again, in the Document Outline (left hand section), under "Placeholders", select "File's Owner". Right click it to see the "Outlets" and "Referencing Outlets" display. Two of the selections under "Outlets" are "view" and "collectionView". Its counter-intuitive, but drag from the "view" selection to the UICollectionView object in the Document Outline.

** If you drag from the "collectionView" selection, you will get the 'loaded the "your_collection_view_controller" nib but didn't get a UICollectionView' error.

When you build and run, the code should now "load" the nib and also "get" the UICollectionView.

Upvotes: 2

pkamb
pkamb

Reputation: 34983

I had this issue when adding a UICollectionViewController as an embedded container view controller on a storyboard.

I switched the container view controller's custom class to be a Collection View Controller. But the view property inside that view controller was still the Xcode boilerplate UIView.

Delete the default UIView within your controller on the storyboard and replace it with a UICollectionView. The outlets should be reconnected automatically.

Upvotes: 4

zeeawan
zeeawan

Reputation: 6905

Jeff Kelley's answer here worked for me for same error.

When you’re using a UICollectionViewController, the view outlet needs to be connected to a UICollectionView in your storyboard; if it’s a UIView of another class, it’ll crash.

So, I used UIViewController with UICollectionView as its property and hooked up with nib.

Upvotes: 2

ppalancica
ppalancica

Reputation: 4277

I think the problem may be because you inherit from UICollectionViewController, you do not have to specify anymore , because it already implements it. Try top delete it, see if it would fix your error.

Also in the Identity Inspector make sure you set the class KidsDetailViewController for the corresponding View Controller.

Upvotes: 2

Related Questions