2013-08-13 4 views
0

Существует UIViewController, которые содержат UICollectionView. Также есть UICollectionViewCell, который имеет собственный класс & XIB (CurrentCell.h, CurrentCell.m, CurrentCell.xib).UICollectionView не показывает ячейку

В моих UIViewController.h:

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

В моей UIViewController.m:

@synthesize collection; 

В 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]; 

В 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; 

Но: если я нажимаю интуитивно клетки didSelectItemAtIndexPath вызовы методов.

EDITED

Теперь все работает!

Я забыл инициализировать ячейку в 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; 
} 

ответ

6

Регистрация перо, а не класс. Вам нужно будет только зарегистрировать класс, если ячейка полностью выполнена в коде. Если вы используете xib, зарегистрируйте его (с регистромNib: forCellWithReuseIdentifier :). Если вы сделаете ячейку в раскадровке, тогда ничего не регистрируйте.

+0

Спасибо! Я просто забыл инициализировать ячейку в классе (см. Мой ответ). – Romowski

+0

@Romowski: Если это решает проблему, отметьте ее как правильный ответ –

+0

См. Мой обновленный вопрос – Romowski

Смежные вопросы