Create UICollectionViewCell subclass with xib

64,100

Solution 1

Make sure the cell on the .xib file know what's the type of the cell.

Select the cell on your interface builder

enter image description here

and then on the identity inspector

enter image description here

Subsequently associate your labels with your properties. (I think you already did that)

Then I'd recommend to verify if you already loaded the .xib file on your cellForItemAtIndexPath: method

NSString *identifier = @"MyCell";    

    static BOOL nibMyCellloaded = NO;

    if(!nibMyCellloaded)
    {
        UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil];
        [cv registerNib:nib forCellWithReuseIdentifier:identifier];
        nibMyCellloaded = YES;
    }

Solution 2

You can find the answer in this document UICollectionView adding UICollectionCell.

Only UICollectionView inside StoryBoard have UICollectionViewCell inside. If use XIB, create a new XIB with CellName.xib, add CollectionViewCell to it, specify name of UICollectionView custom class. After that use registerNib.Sample code: https://github.com/lequysang/TestCollectionViewWithXIB

Share:
64,100

Related videos on Youtube

Piero
Author by

Piero

Updated on July 09, 2022

Comments

  • Piero
    Piero almost 2 years

    I'm trying to create a UICollectionViewCell subclass with linked a xib, I have do this: I have create a new xib file and I have add a UICollectionViewCell in it, then I have create this subclass file:

    @interface MyCell : UICollectionViewCell
    
    @property (weak, nonatomic) IBOutlet UILabel *label;
    @end
    

    Also I have linked in the file owner custom class the MyCell class in interface builder, and I have added a UILabel, then in my UICollectionView viewDidLoad I do this:

    [self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];
    
    UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"];
    

    As well as in this:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    
    
    cell.label.text = @"Cell Text";
    
    
    return cell;
    }
    

    However this doesn't work, I receive this error:

    *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x907eca0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'
    

    What did I do wrong? How can I connect a UICollectionViewCell subclass to a xib, and display it in a UICollectionView?

    EDIT:

    i have do this:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    
    NSString *identifier = @"MyCell";
    
    static BOOL nibMyCellloaded = NO;
    
    if(!nibMyCellloaded)
    {
        UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil];
        [cv registerNib:nib forCellWithReuseIdentifier:identifier];
        nibMyCellloaded = YES;
    }
    
    MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    
    
    cell.labelCell.text = @"Text";
    
    
    return cell;
    }
    
    • topwik
      topwik about 11 years
      so in the case of using a xib with a connected class, it seems you have to call collectionView register on both the nib file and the class you linked with the xib? is your workflow invoking the cell class' initWithFrame initializer?
    • Hai Feng Kao
      Hai Feng Kao over 10 years
      Your "edited code" is buggy. Static variables inside a member function are shared by all instances. If you have multiple view controller instances, only the first one will call registerNib.
  • Piero
    Piero over 11 years
    i have edited my question...take a look please...
  • Piero
    Piero over 11 years
    no i'm sorry, i have enlarge the cell and know works...
  • topwik
    topwik about 11 years
    can't you just call the registerNib once on viewDidLoad instead of doing the check every time the collection view is asking for a cell?
  • Ali
    Ali almost 11 years
    Thanks. your code is the only one that worked for me. I'm just wondering why use make the UILabel property of cell weak? I thought all IBOutlets must be strong.
  • XCool
    XCool over 10 years
    @Ali regarding your question: stackoverflow.com/a/7729141/351810
  • Hai Feng Kao
    Hai Feng Kao about 9 years
    Using static is a bad practice. The static variable is shared for all instances. The second view controller of the same class will not load the .xib file because nibMyCellloaded will be set to YES by the first one.
  • Eugene Tiutiunnyk
    Eugene Tiutiunnyk almost 9 years
    Important note here is that .XIB File's Owner should NOT be set to MyCell, but only the root element Class. Otherwise the solution doesn't work.