title: UICollectionView cell selection and cell reuse
body: <p>Upon cell selection, I want to handle changing the cell appearance. I figured the delegate method <code>collectionView:didSelectItemAtIndexPath:</code> &amp; <code>collectionView:didDeselectItemAtIndexPath:</code> is where I should edit the cell.</p>
  
  <pre><code>-(void)collectionView:(UICollectionView *)collectionView 
         didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  
      DatasetCell *datasetCell = 
        (DatasetCell *)[collectionView cellForItemAtIndexPath:indexPath];
  
      [datasetCell replaceHeaderGradientWith:[UIColor skyBlueHeaderGradient]];
      datasetCell.backgroundColor = [UIColor skyBlueColor];
  }
  </code></pre>
  
  <h3>and</h3>
  
  <pre><code>-(void)collectionView:(UICollectionView *)collectionView 
         didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
  
      DatasetCell *datasetCell = 
        (DatasetCell *)[collectionView cellForItemAtIndexPath:indexPath];
  
      [datasetCell replaceHeaderGradientWith:[UIColor grayGradient]];
      datasetCell.backgroundColor = [UIColor myDarkGrayColor];
  }
  </code></pre>
  
  <p>This works fine, except when the cell gets reused. If I select cell at index (0, 0), it changes the appearance but when I scroll down, there is another cell in the selected state.</p>
  
  <p>I believe I should use the <code>UICollectionViewCell</code> method <code>-(void)prepareForReuse</code> to prep the cell for resuse (ie, set the cell appearance to non selected state) but its giving me difficulties.</p>
  
  <pre><code>-(void)prepareForReuse {
      if ( self.selected ) {
          [self replaceHeaderGradientWith:[UIColor skyBlueHeaderGradient]];
          self.backgroundColor = [UIColor skyBlueColor];
      } else {
          [self replaceHeaderGradientWith:[UIColor grayGradient]];
          self.backgroundColor = [UIColor myDarkGrayColor];
      }
  }
  </code></pre>
  
  <p>When I scroll back to the top, the cell at index (0, 0) is in the deselected state.</p>
  
  <p>When I just used the cell.backgroundView property, to prevent this from happening was to:</p>
  
  <pre><code>-(void)prepareForReuse {
      self.selected = FALSE;
  }
  </code></pre>
  
  <p>and the selection state worked as intended.</p>
  
  <p>Any ideas?</p>