2014-04-29 13 views
5

Sto utilizzando uno collectionView per mostrare le immagini dopo il recupero da Coredatabase. Tuttavia, nessuno dei metodi viene chiamato tranne numberOfItemsInSection & numberOfSectionsInCollectionView. I delegati e l'origine dati sono impostati correttamente.cellForItemAtIndexPath, sizeForItemAtIndexPath, insetForSectionAtIndex non viene richiamato iOS

Anche l'identificatore di cella è fotoCell e classe è JLTPhotoCell.

- (void)viewDidLoad 
     { 
      [super viewDidLoad]; 
      _thumbNails = [[NSArray alloc]init]; // its declared ,don't worry about this 
      _thumbNails = [[LADataModelController getSingleton] getAllSignatures]; 

      UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 
      [layout setItemSize:(CGSizeMake(50,50))]; 
      [_collectionView setCollectionViewLayout:layout]; 
      self.collectionView.delegate = self; 
      self.collectionView.dataSource = self; 
      [_collectionView registerClass:[JLTPhotoCell class] forCellWithReuseIdentifier:@"photoCell"]; 
      [self.view addSubview:self.collectionView]; 
      self.collectionView.allowsSelection = true; 
    } 

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section 
{ 
    NSLog(@"count %i" , _thumbNails.count); 
    return _thumbNails.count; 
} 

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (_thumbNails.count > 0) 
    { 
     JLTPhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath]; 

     NSData *thumbnailData = ((LASignature*)[_thumbNails objectAtIndex:indexPath.row]).signatureImage; 
     UIImage* thumbnailImage = [UIImage imageWithData:thumbnailData]; 
     [cell setThumbnail:thumbnailImage]; 
     NSLog(@"image:%@",thumbnailImage); 

     if (!_buttonSelect.hidden) 
     { 
      [cell performSelected:FALSE]; 
     } 

     return cell; 
    } 

    return nil; 
} 

#pragma mark UICollectionViewDelegate Methods 

-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    _lastSelectedSign = [_thumbNails objectAtIndex:indexPath.row]; 

    if (!self.buttonSelect.hidden) 
    { 
     JLTPhotoCell *cell = (JLTPhotoCell *)[_collectionView cellForItemAtIndexPath:indexPath]; 
     cell.selected = FALSE; 

    } 
    else 
    { 
     [_selectedImages addObject:[_thumbNails objectAtIndex:indexPath.row]]; 
     JLTPhotoCell *cell = (JLTPhotoCell *)[_collectionView cellForItemAtIndexPath:indexPath]; 
     [cell performSelected:TRUE]; 
     // _buttonDelete.enabled = _selectedImages.count > 0; 
    } 

} 

-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [_selectedImages removeObject:[_thumbNails objectAtIndex:indexPath.row]]; 

    JLTPhotoCell *cell = (JLTPhotoCell *)[_collectionView cellForItemAtIndexPath:indexPath]; 
    [cell performSelected:FALSE]; 
    _buttonDelete.enabled = _selectedImages.count > 0; 

} 

#pragma mark - UICollectionViewDelegateFlowLayout 
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(50,50) ; 
} 

-(UIEdgeInsets) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{ 
    collectionViewLayout.minimumInteritemSpacing=1; 
    collectionViewLayout.minimumLineSpacing =2; 
    return UIEdgeInsetsMake(1, 1, 1, 1); 
} 

jltPhotoCell Class 
-(void) setThumbnail:(UIImage *) thumbnail 
{ 
    [self setThumbnail:thumbnail andWithSize:(CGSizeMake(50,50))]; 
} 

-(void) setThumbnail:(UIImage *) thumbnail andWithSize:(CGSize) size 
{ 
    UIImageView *thumbImageView = [[UIImageView alloc] initWithImage:thumbnail]; 
    thumbImageView.frame = CGRectMake(0,0,size.width,size.height); 
    thumbImageView.userInteractionEnabled = YES; 
    [self.contentView addSubview:thumbImageView]; 
} 

-(void) performSelected:(bool)selected 
{ 
    if (selected) 
    { 
     UIImageView *deleteImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"delete"]]; 
     deleteImageView.frame = CGRectMake(self.contentView.frame.size.width - 35, 3, 32, 32); 
     deleteImageView.tag = 123; 
     [self.contentView addSubview:deleteImageView]; 
    } 
    else 
    { 
     UIView *v = [self.contentView viewWithTag:123]; 
     v.hidden = YES; 
     [self.contentView bringSubviewToFront:v]; 
     [v removeFromSuperview]; 
    } 
} 

Qualsiasi suggerimento sarebbe apprezzato. Grazie.

+0

Cosa mostra questo registro, NSLog (@ "count% i", _thumbNails.count)? – rdelmar

+0

il registro mostra il conteggio 1, il n. di immagine recuperata. – LUI

+0

Avete controllato NSLog (@ "% @", _collectionView); –

risposta

1

Mi sono imbattuto nello stesso problema. Sono stato in grado di risolverlo chiamando il layout invalidateLayout dopo aver assegnato l'origine dati.

4

Come @highlycaffeated mi sono imbattuto in questo problema. Tuttavia chiamare invalidateLayout non ha funzionato per me.

Tuttavia, l'assegnazione del delegato DOPO l'origine dati elaborata.

ho cambiato:

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

con:

self.collectionView.dataSource = self; 
self.collectionView.delegate = self; 
+0

Entrambi i gruppi di linee sono uguali, ... – Kyle

+0

Grazie, ho modificato la risposta. – dynamokaj

+1

Ottima risposta, peccato che Apple non ci abbia pensato, o nemmeno menzionato nella documentazione ... – dulgan

0

Solo per le persone che incontrano problemi simili con UICollectionViews soprattutto quando si fa a livello di codice. Quindi ho avuto lo stesso problema, alla fine ho scoperto che avevo zero elementi nell'array che si supponeva fosse popolato in collectionView. Quindi, dopo aver letto la documentazione, dice che le Viste Raccolta non verranno mostrate a meno che non ci sia almeno un elemento da mostrare dall'array, altrimenti ti darà lo schermo nero fino in fondo. Un altro problema che può verificarsi in questo scenario è che sizeForItemAtIndexPath verrà chiamato prima di cellForItemAtIndexPath per il contenuto della cella come etichette e le immagini non prenderanno le loro altezze ma l'altezza della cella stessa. spero che questa risposta aiuti le persone in giro.

Problemi correlati