0

I created a custom UICollectionViewCell as you can see, the creation of every single cell without any problems and can find the xib, but nothing is displayed on the screen.

I have connected both the datasource that the delegates in the xib of ProgrammaCell. Here is my code ....

my CollectionView is called "griglia" and is connected in the xib

@interface GrigliaProgrammiViewController : UIViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{    XMLParser *xmlParser;
}

....

@end

- (void)viewDidLoad
{
[super viewDidLoad];
   .....
[self.griglia registerClass:[ProgrammaCell class] forCellWithReuseIdentifier:@"prg"];
self.griglia.dataSource = self;
self.griglia.delegate = self;
xmlParser = [[XMLParser alloc]
loadXMLByURL:@"..."];
[self.griglia reloadInputViews];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:    (NSInteger)section {
return [[xmlParser programmi] count];}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}


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


ProgrammaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"prg" forIndexPath:indexPath];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Programma" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

Programmi *programma = [[xmlParser programmi] objectAtIndex:indexPath.row];

cell.titolo.text= programma.titolo;
cell.giorno.text = programma.programma_giorno;
cell.orario.text = programma.programma_orario;


UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = cell.immagine.bounds;
[cell.immagine addSubview:activityIndicator];
[cell.immagine setImageWithURL:[NSURL URLWithString:programma.programma_copertina]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                      [activityIndicator removeFromSuperview];
                      }];
return cell;
}
Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
user3019841
  • 49
  • 1
  • 6

3 Answers3

0

How did you initialize your UICollectionView? What type of layout are you using? I find that sometimes the cellForItemAtIndexPath is not being called if the itemsize of the lay out is too big.

See question UICollectionView's cellForItemAtIndexPath is not being called

Community
  • 1
  • 1
djshiow
  • 1,033
  • 11
  • 7
0

Could you try to use this code snippet in (void)viewDidLoad

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Programma" owner:self options:nil];
[self.griglia registerNib:nib forCellWithReuseIdentifier:@"prg"];

instead of your original code:

[self.griglia registerClass:[ProgrammaCell class] forCellWithReuseIdentifier:@"prg"];

and

ProgrammaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"prg" forIndexPath:indexPath];
//if (cell == nil) {
//    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Programma" owner:self options:nil];
//    cell = [nib objectAtIndex:0];
//}

My guess is that the registerClass:forCellWithReuseIdentifier: method register your class as reusable cell, but your cell class only describes what the cell should do (the behavior of the cell). Only the Nib file describes how the cell should be presented (the UI of the cell).

Just my guess, give it a try. :)

yikai
  • 46
  • 3
  • thanks for comment.. I try it..and get other error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ProgrammaCell instantiateWithOwner:options:]: unrecognized selector sent to instance 0xe13a380' – user3019841 Nov 22 '13 at 08:19
  • the error is in ProgrammaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"prg" forIndexPath:indexPath]; – user3019841 Nov 22 '13 at 08:20
  • It SHOULD like be `[tableView registerNib:[UINib nibWithNibName:@"JXVideoLikeMsgCell" bundle:nil] forCellReuseIdentifier:JXVideoLikeMsgCellID];`, NOT load from NSBundle. – Leo Feb 17 '17 at 03:11
0

Load cell from nib SHOULD do like this:

[tableView registerNib:[UINib nibWithNibName:@"YourCellNibName" bundle:nil] forCellReuseIdentifier:JXVideoLikeMsgCellID];

SHOULD NOT load from NSBundle:

[[NSBundle mainBundle] loadNibNamed:@"YourCellNibName" owner:nil options:nil];


Same to UICollectionView.

Leo
  • 1,545
  • 13
  • 24