title: collectionView:cellForItemAtIndexPath mixes up images, drawn with drawRect:
body: <p>I decided to draw my UICollectionViewCell's with <code>drawRect:</code> instead of the custom xib files for performance (as I have shadows and label shadows).</p>
  
  <p>For some reason, since I changed this, my cells not draw with different images each time they are reloaded into the <code>UICollectionView</code>.</p>
  
  <p>When I log the name of the image it is set to load in both the <code>collectionView:cellForItemAtIndexPath</code> and <code>drawRect:</code> it supplies the correct name of the image and the correct index path, yet when I load the app, it muddles them up on scrolling, even though initially, they have loaded correctly.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>viewDidLoad</strong></p>
  
  <pre><code>//  Collection cells
  //
  [self.collectionView registerClass:[CluelessSymbolCell class] forCellWithReuseIdentifier:@"Symbol cell"];
  </code></pre>
  
  <p><strong>collectionView:cellForItemAtIndexPath</strong></p>
  
  <pre><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  {
      CluelessSymbolCell *cell = (CluelessSymbolCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Symbol cell" forIndexPath:indexPath];
  
      CluelessClue *clue = [self.clues objectAtIndex:indexPath.row];
  
      NSLog(@"(%@) Image: %@", indexPath, [clue.imageNames objectAtIndex:0]);
  
      [cell setClue:clue];
  
      return cell;
  }
  </code></pre>
  
  <p><strong>drawRect: (In cell subclass 'CluelessSymbolCell')</strong></p>
  
  <pre><code>- (void)drawRect:(CGRect)rect
  {
      NSString *imageName = [NSString stringWithFormat:@"%@-icon-iphone", [self.clue.imageNames objectAtIndex:0]];
  
      //  Cell
      //
      [self.contentView.layer setShadowColor:[UIColor blackColor].CGColor];
      [self.contentView.layer setShadowOpacity:0.3f];
      [self.contentView.layer setShadowRadius:2.0f];
      [self.contentView.layer setShadowOffset:CGSizeZero];
      [self.contentView.layer setShadowPath:[UIBezierPath bezierPathWithRect:self.layer.bounds].CGPath];
  
      //      Image
      //
      CALayer *imageLayer = [CALayer layer];
      [imageLayer setFrame:rect];
      [imageLayer setContents:(id)[[UIImage imageNamed:imageName] CGImage]];
  
      [self.contentView.layer addSublayer:imageLayer];
  }
  </code></pre>
  
  <p><em><strong>Edit</em></strong></p>
  
  <p>On logging the sublayers of the cell's content view, I can see that each cell's contentview sublayer has moved to the next cell, what is the reason for this and how do I overcome it?</p>
  
  <p><strong>Log:</strong></p>
  
  <pre><code>2013-02-22 23:41:08.064 Clueless[3462:c07] &lt;NSIndexPath 0x845cb90&gt; 2 indexes [0, 0], (null)
  2013-02-22 23:41:08.065 Clueless[3462:c07] &lt;NSIndexPath 0x84435f0&gt; 2 indexes [0, 1], (null)
  2013-02-22 23:41:08.066 Clueless[3462:c07] &lt;NSIndexPath 0x8446f50&gt; 2 indexes [0, 2], (null)
  2013-02-22 23:41:08.066 Clueless[3462:c07] &lt;NSIndexPath 0x843a060&gt; 2 indexes [0, 3], (null)
  2013-02-22 23:41:08.067 Clueless[3462:c07] &lt;NSIndexPath 0x846bc70&gt; 2 indexes [0, 4], (null)
  2013-02-22 23:41:08.067 Clueless[3462:c07] &lt;NSIndexPath 0x845cd20&gt; 2 indexes [0, 5], (null)
  2013-02-22 23:41:08.068 Clueless[3462:c07] &lt;NSIndexPath 0x845ce60&gt; 2 indexes [0, 6], (null)
  2013-02-22 23:41:08.068 Clueless[3462:c07] &lt;NSIndexPath 0x846ce40&gt; 2 indexes [0, 7], (null)
  2013-02-22 23:41:08.069 Clueless[3462:c07] &lt;NSIndexPath 0x846c9b0&gt; 2 indexes [0, 8], (null)
  2013-02-22 23:41:08.069 Clueless[3462:c07] &lt;NSIndexPath 0x846d880&gt; 2 indexes [0, 9], (null)
  2013-02-22 23:41:08.070 Clueless[3462:c07] &lt;NSIndexPath 0x846db20&gt; 2 indexes [0, 10], (null)
  2013-02-22 23:41:08.071 Clueless[3462:c07] &lt;NSIndexPath 0x846df30&gt; 2 indexes [0, 11], (null)
  2013-02-22 23:41:15.105 Clueless[3462:c07] &lt;NSIndexPath 0x846ec60&gt; 2 indexes [0, 12], (null)
  2013-02-22 23:41:15.106 Clueless[3462:c07] &lt;NSIndexPath 0x846efb0&gt; 2 indexes [0, 13], (null)
  2013-02-22 23:41:15.107 Clueless[3462:c07] &lt;NSIndexPath 0x846f6d0&gt; 2 indexes [0, 14], (null)
  2013-02-22 23:41:15.753 Clueless[3462:c07] &lt;NSIndexPath 0x82492e0&gt; 2 indexes [0, 15], (
      "&lt;CALayer: 0x8495840&gt;"
  )
  2013-02-22 23:41:15.754 Clueless[3462:c07] &lt;NSIndexPath 0x827f750&gt; 2 indexes [0, 16], (
      "&lt;CALayer: 0x8491010&gt;"
  )
  2013-02-22 23:41:15.754 Clueless[3462:c07] &lt;NSIndexPath 0x827f5c0&gt; 2 indexes [0, 17], (
      "&lt;CALayer: 0x8493ef0&gt;"
  )
  2013-02-22 23:41:15.986 Clueless[3462:c07] &lt;NSIndexPath 0x845cb90&gt; 2 indexes [0, 0], (
      "&lt;CALayer: 0x8493ef0&gt;"
  )
  2013-02-22 23:41:15.987 Clueless[3462:c07] &lt;NSIndexPath 0x84435f0&gt; 2 indexes [0, 1], (
      "&lt;CALayer: 0x8491010&gt;"
  )
  2013-02-22 23:41:15.987 Clueless[3462:c07] &lt;NSIndexPath 0x8446f50&gt; 2 indexes [0, 2], (
      "&lt;CALayer: 0x8495840&gt;"
  )
  2013-02-22 23:41:26.161 Clueless[3462:c07] &lt;NSIndexPath 0x827d970&gt; 2 indexes [0, 12], (
      "&lt;CALayer: 0x8241e20&gt;"
  )
  2013-02-22 23:41:26.162 Clueless[3462:c07] &lt;NSIndexPath 0x824e9d0&gt; 2 indexes [0, 13], (
      "&lt;CALayer: 0x827bc60&gt;"
  )
  2013-02-22 23:41:26.163 Clueless[3462:c07] &lt;NSIndexPath 0x827d030&gt; 2 indexes [0, 14], (
      "&lt;CALayer: 0x8242fb0&gt;"
  )
  2013-02-22 23:41:26.776 Clueless[3462:c07] &lt;NSIndexPath 0x82492e0&gt; 2 indexes [0, 15], (
      "&lt;CALayer: 0x8491010&gt;"
  )
  2013-02-22 23:41:26.777 Clueless[3462:c07] &lt;NSIndexPath 0x827f750&gt; 2 indexes [0, 16], (
      "&lt;CALayer: 0x8493ef0&gt;"
  )
  2013-02-22 23:41:26.778 Clueless[3462:c07] &lt;NSIndexPath 0x827f5c0&gt; 2 indexes [0, 17], (
      "&lt;CALayer: 0x8495840&gt;"
  )
  2013-02-22 23:41:26.926 Clueless[3462:c07] &lt;NSIndexPath 0x845cb90&gt; 2 indexes [0, 0], (
      "&lt;CALayer: 0x8495840&gt;"
  )
  2013-02-22 23:41:26.927 Clueless[3462:c07] &lt;NSIndexPath 0x84435f0&gt; 2 indexes [0, 1], (
      "&lt;CALayer: 0x8493ef0&gt;"
  )
  2013-02-22 23:41:26.928 Clueless[3462:c07] &lt;NSIndexPath 0x8446f50&gt; 2 indexes [0, 2], (
      "&lt;CALayer: 0x8491010&gt;"
  )
  </code></pre>