Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

xcode - UICollectionView doesn't contain UICollectionViewCell in IB

I'm trying to add a UICollectionView to a .nib file in IB. I've worked through a few tutorials and had no problems.

In my existing app, when I drag a collection view into a view in IB and then expand the object, the CollectionView Flow Layout is there , but there is no collection view cell. I also cannot drag and drop a cell into the collection view. Also the collection view is displayed differently, showing an image of a grid of cells instead of the normal white box.

I've tried creating a new view controller with nib, and have the same results when adding a collectionView.

This app WAS targeting iOS 4.2, but I've changed the deployment target to iOS 6.

In any newly created projects I don't get this behavior.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can't tell you why it does not work, but for me the subclass approach from this blog post solved the problem:

http://www.adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial.php

here is a short summary:

  • create a new class MyViewCell which extends UICollectionViewCell and create properties, actions, outlets and methods as needed.

@interface MyViewCell : UICollectionViewCell

  • create a View (xib file) with a Collection View Cell as its root object (and delete the object which is created by default).
  • in the attributes set the custom class of this Collection View Cell to your extended class MyViewCell (instead of the default UICollectionViewCell).
  • in the attributes under Collection Reusable View define an Identifier, e.g. myCell, you will need this later to register your call.
  • go back to your custom class and modify the inithWithFrame method to load and use your created view.

    - (id)initWithFrame:(CGRect)frame 
    { 
       self = [super initWithFrame:frame]; 
       if (self) 
       { 
         // Initialization code 
         NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CVCell" owner:self options:nil];
    
         if ([arrayOfViews count] < 1) { return nil; }
    
         if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) { return nil; }
    
         self = [arrayOfViews objectAtIndex:0];
       }
       return self;
    }
    
  • then you can register this class to be used by the collectionView with

[self.collectionView registerClass:[MyViewCell class] forCellWithReuseIdentifier:@"myCell"];


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...