5

Ich verwende eine collectionView, um Bilder nach dem Abrufen von Coredatabase anzuzeigen. Jedoch werden keine der Verfahren aufgerufen, außer numberOfItemsInSection & numberOfSectionsInCollectionView. Die Delegaten und die Datenquelle sind ordnungsgemäß festgelegt.cellForItemAtIndexPath, sizeForItemAtIndexPath, insetForSectionAtIndex wird nicht aufgerufen iOS

Auch die Kennung der Zelle ist PhotoCell und Klasse ist 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]; 
    } 
} 

Jeder Vorschlag wäre willkommen. Vielen Dank.

+0

Was zeigt dieses Protokoll, NSLog (@ "count% i", _thumbNails.count)? – rdelmar

+0

Protokoll zeigt Anzahl 1, die Nr. des abgerufenen Bildes. – LUI

+0

Haben Sie den NSLog überprüft (@ "% @", _collectionView); –

Antwort

1

Ich lief auf das gleiche Problem. Ich konnte es lösen, indem ich nach Zuweisung der Datenquelle die invalidateLayout des Layouts aufruft.

4

Als @highcaffeinated lief ich auf dieses Problem zu. Das Aufrufen von invalidateLayout funktionierte jedoch nicht für mich.

Allerdings Zuweisung der Stellvertretung nach der Datenquelle funktioniert.

I geändert:

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

mit:

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

Beide Sätze von Linien sind gleich, ... – Kyle

+0

Danke, ich habe die Antwort bearbeitet. – dynamokaj

+1

Große Antwort, das ist schade, dass Apple nicht darüber nachgedacht hat, oder sogar erwähnen es in der Dokumentation ... – dulgan

0

Gerade für die Menschen, die besonders ähnliche Probleme mit UICollectionViews begegnen werden, wenn es programmatisch zu tun. So hatte ich das gleiche Problem, schließlich fand ich heraus, dass ich null Elemente in dem Array hatte, das in der CollectionView aufgefüllt werden sollte. Nach dem Lesen der Dokumentation heißt es also, dass Collection Views nur dann angezeigt werden, wenn mindestens ein Element aus dem Array angezeigt wird. Andernfalls wird der gesamte Bildschirm schwarz. Ein weiteres Problem, das in diesem Szenario auftreten kann, ist, dass der sizeForItemAtIndexPath vor dem cellForItemAtIndexPath aufgerufen wird. Daher wird der Inhalt der Zelle, wie Labels und Bilder, nicht ihre eigene Höhe annehmen, sondern die Höhe der Zelle selbst. Ich hoffe, diese Antwort wird den Menschen helfen.