UICollectionView does not show cell

There is a UIViewController that contain a UICollectionView . There is also a UICollectionViewCell that has its own class and XIB (CurrentCell.h, CurrentCell.m, CurrentCell.xib).

In my UIViewController.h :

 @property (strong, nonatomic) IBOutlet UICollectionView *collection; 

In my UIViewController.m :

 @synthesize collection; 

In viewDidLoad :

 [self.collection registerClass:[CurrentCell class] forCellWithReuseIdentifier:@"cellCurRecipe"]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; [flowLayout setItemSize:CGSizeMake(120, 90)]; [flowLayout setSectionInset:UIEdgeInsetsMake(5, 10, 5, 10)]; [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; [self.collection setCollectionViewLayout:flowLayout]; 

In cellForItemAtIndexPath :

 static NSString *CellIdentifier = @"cellCurRecipe"; CurrentCell *cell = (CurrentCell *)[self.collection dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; Recipes *recipe = [currRecipesArray objectAtIndex:indexPath.item]; [cell.image setImage: recipe.image]; return cell; 

But: if I indirectly connect to the didSelectItemAtIndexPath method call .

edited

Now everything works!

I forgot to initialize the cell in CurrentCell.m:

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CurrentCell" 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; } 
0
ios uicollectionview uicollectionviewcell
source share
1 answer

Register the thread, not the class. You will only need to register the class if the cell is fully executed in code. If you are using xib, register it (with register Nib: forCellWithReuseIdentifier :). If you make a cell in the storyboard, then do not register anything.

+6
source share

All Articles