title: Reused cells in a UICollectionView show multiple UIImageViews when they should only show one
body: <p>Im having a problem with my <code>UICollectionView</code>. Initially it displays fine, showing a grid of cells, each cell with a single <code>UIImageView</code>. These <code>UIImageViews</code> are showing PNGs with transparency that are stored in the app's bundle. </p>
  
  <p>My problem is, once the <code>UICollectionView</code> has been scrolled, some of the cells seem to be corrupt.</p>
  
  <p>A corrupt cell shows multiple images stacked on top of each other, the top most image is the one it should be showing, and images underneath are the ones that should be used in other cells.</p>
  
  <p>My best guess is that this has something to do with the way cells inside a <code>UICollectionView</code> are reused, but I am open to suggestions.</p>
  
  <p>This is the delegate code I use for creating cells within the <code>UICollectionView</code>:</p>
  
  <pre><code>// creates the individual cells to go in the menu view
  - (UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  
      // create collection view cell
      UICollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  
      // create a uiview where we can place all views that need to go into this cell
      UIView * contents=[[UIView alloc] initWithFrame:cell.contentView.bounds];
      [contents setBackgroundColor:[UIColor clearColor]];
      [cell.contentView addSubview:contents];
  
      // add a button image
      NSString * buttonPath=[[NSBundle mainBundle] pathForResource:@"button" ofType:@"png" inDirectory:[[buttons objectAtIndex:indexPath.row] objectForKey:@"name"]];
      UIImage * button=[UIImage imageWithContentsOfFile:buttonPath];
      UIImageView * buttonView=[[UIImageView alloc] initWithImage:button];
      [buttonView setContentMode:UIViewContentModeScaleAspectFit];
      [buttonView setFrame:contents.bounds];
      [contents addSubview:buttonView];
  
      // set tag to the indexPath.row so we can access it later
      [cell setTag:indexPath.row];
  
      // add interactivity
      UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
      [tap setNumberOfTapsRequired:1];
      [cell addGestureRecognizer:tap];
  
      // return the cell
      return cell;
  
  }
  </code></pre>
  
  <p>I can provide more code if required.</p>
  
  <p>How can I stop the cells from corrupting?</p>