title: Change Background of UICollectionView Cell on Tap
body: <p>I have a UICollectionView that I have created programmatically. I would like for the collection view to behave in the following way:</p>
  
  <pre><code>1. User touches cell
  2. Cell background color changes
  3. User releases touch
  4. Cell background color changes
  </code></pre>
  
  <p>This should be a quick color change that happens just before the selector related to the tap action is executed in which the viewcontroller containing the collection view is popped off the stack.</p>
  
  <p>I have been looking at this question: <a href="https://stackoverflow.com/questions/17209863/uicollectionview-cell-change-background-while-tap">UICollectionView cell change background while tap</a></p>
  
  <p>in which there is the following summary of methods to use for this purpose:</p>
  
  <pre><code>// Methods for notification of selection/deselection and highlight/unhighlight events.
  // The sequence of calls leading to selection from a user touch is:
  //
  // (when the touch begins)
  // 1. -collectionView:shouldHighlightItemAtIndexPath:
  // 2. -collectionView:didHighlightItemAtIndexPath:
  //
  // (when the touch lifts)
  // 3. -collectionView:shouldSelectItemAtIndexPath: or -    collectionView:shouldDeselectItemAtIndexPath:
  // 4. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath:
  // 5. -collectionView:didUnhighlightItemAtIndexPath:
  </code></pre>
  
  <p>I am assuming I only need to implement one of the above methods from 'when touch begins' and 'when touch ends.' But no matter what I do, it appears that a background color changes and then remains changed. Here is an example of something I attempted which did not work:</p>
  
  <pre><code>- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
   {
     //pop vc 
   }
  
  - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
  {
    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor redColor];
  }
  
  - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  {
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor greenColor];
  }
  </code></pre>
  
  <p>This results in the cell background color being changed only to red. I also looked at this question: <a href="https://stackoverflow.com/questions/15330844/uicollectionview-select-and-deselect-issue">UICollectionView Select and Deselect issue</a> and tried implementing [UICollectionView selectItemAtIndexPath:animated:scrollPosition:] and calling it inside of didSelectItemAtIndexPath, but this did not work either. Collection view data source and delegate are set.</p>