user908015
user908015

Reputation:

UICollectionView Cell nib not loading

I'm using a custom subclass of UICollectionViewFlowLayout that allows reordering of cells. Source code here.

The issue I am running into is that the nib file I created for the Collection View does not load despite registering it and using the correct identifier. Here are the relevant code snippets.

This is in the view controller .m file:

- (void)loadView
{
    self.reorderLayout = [[LXReorderableCollectionViewFlowLayout alloc] init];

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero
                                             collectionViewLayout:self.reorderLayout];

    UINib *nib = [UINib nibWithNibName:@"CatagoryViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"CatagoryViewCellID"];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Catagory *c = [[[CatagoryStore defaultStore] allCatagories]
                   objectAtIndex:[indexPath item]];

    CatagoryViewCell *cell = (CatagoryViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CatagoryViewCellID" forIndexPath:indexPath];

    [cell setController:self];

    [[cell nameLabel] setText:c.title];
    return cell;
}

These are the only two places I reference the nib.

Here is the cell nib: enter image description here

This is what it looks like in the simulator and device:

enter image description here

I tried using similar code and the original UICollectionViewFlowLayout class and it works fine. Any insight is greatly appreciated!

Upvotes: 3

Views: 5875

Answers (1)

Angel Gzid
Angel Gzid

Reputation: 71

Try with this "initWithFrame" methods inside your CollectionViewCell.m

-(id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];

if (self) {
    NSString *cellNibName = @"YourCollectionViewCell";
    NSArray *resultantNibs = [[NSBundle mainBundle] loadNibNamed:misVentasCellNibName owner:nil options:nil];

    if ([resultantNibs count] < 1) {
        return nil;
    }

    if (![[resultantNibs objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
        return nil;
    }
    self = [resultantNibs objectAtIndex:0];
}

return self;    
}

Then in the class you have de collection view add:

[collectionView registerClass:[YourCollectionViewCell class] forCellWithReuseIdentifier:@"YourCollectionViewCell"];

And Finally

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

// Setup cell identifier
static NSString *cellIdentifier = @"YourCollectionViewCell";

YourCollectionViewCell *cell = (YourCollectionViewCell *)[cvArticles dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
}

Remember you must insert in your.xib the same cellIdentifier you are using in the Identifier field of the Collection view!!!!!!!!

Upvotes: 6

Related Questions