title: Simple UICollectionView to show images behaves odd: Some Images are displayed correct, some at wrong position, some missing at all
body: <p>I want to show images in a grid on iPhone using a UICollectionView, showing 3 images a row. For a "dead simple test" (as I thought), I've added 15 JPG images to my project, so that they'll be in my bundle and I can load them simply via [UIImage imageNamed:...].</p>
  
  <p>I think I've done everything correct (setting up &amp; registering UICollectionViewCell subclass, use of UICollectionViewDataSource Protocol methods), however, the UICollectionView behaves very weird:</p>
  
  <p>It shows only a few of the images in the following pattern:
  First line shows image 1 &amp; 3, second line is blank, next line like the first again (image 1 &amp; 3 showing properly), fourth line blank, and so on...</p>
  
  <p>If I push a button in my NavBar that triggers [self.collectionView reloadData], random cells appear or disappear. What drives me nuts is that it's not only an issue of images appear or not. Sometime, images also swap between the cells, i.e. they appear for a indexPath they are definitely not wired up!</p>
  
  <p>Here is my code for the cell:</p>
  
  <pre><code>@interface AlbumCoverCell : UICollectionViewCell
  @property (nonatomic, retain) IBOutlet UIImageView *imageView;
  @end
  
  @implementation AlbumCoverCell
  @synthesize imageView = _imageView;
  - (id)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self) {
          _imageView = [[UIImageView alloc] initWithFrame:frame];
          [self.contentView addSubview:_imageView];
      }
      return self;
  }
  
  - (void)dealloc
  {
      [_imageView release];
      [super dealloc];
  }
  
  - (void)prepareForReuse
  {
      [super prepareForReuse];
      self.imageView.image = nil;
  }
  @end
  </code></pre>
  
  <p>Part of the code for my UICollectionViewController subclass, where 'imageNames' is an NSArray holding all jpg filenames:</p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
      [self.collectionView registerClass:[AlbumCoverCell class] forCellWithReuseIdentifier:kAlbumCellID];
  }
  
  #pragma mark - UICollectionViewDataSource Protocol methods
  - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  {
      return [self.imageNames count];
  }
  
  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  {
      AlbumCoverCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kAlbumCellID forIndexPath:indexPath];
      NSString *imageName = [self.imageNames objectAtIndex:indexPath.row];
      NSLog(@"CV setting image for row %d from file in bundle with name '%@'", indexPath.row, imageName);
      cell.imageView.image = [UIImage imageNamed:imageName];
  
      return cell;
  }
  
  #pragma mark - UICollectionViewDelegateFlowLayout Protocol methods
  - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
  {
      return CGSizeMake(100, 100);
  }
  
  - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
  {
      return UIEdgeInsetsMake(0, 0, 0, 0);
  }
  </code></pre>
  
  <p>From the NSLog statement in cellForItemAtIndexPath: I can see that the method is called for all of the cells (not only the one's displayed) and that the mapping between indexPath.row and filename is correct.</p>
  
  <p>Has anybody an idea what could cause this weird behavior?</p>