title: Swift: UICollectionView selecting cell indexPath issues
body: <p>I am trying to do a <code>Collection View</code> whereby someone selects a <code>cell</code> and for each selection it takes them to another <code>View Controller</code> that holds content of the selection. However I'm running into difficulties as once I apply this line of code to <code>didSelectItemAtIndexPath</code>; </p>
  
  <pre><code>self.performSegueWithIdentifer("showDetail", sender: self)
  </code></pre>
  
  <p>and then run it in the Simulator the <code>cell</code> selection is working according the <code>indexPath</code> but its remembering the selections each time I select new <code>cell</code>. So for example each <code>cell</code> has a photo and label and if I select the first <code>cell</code> in the <code>indexPath</code> the <code>segue</code> takes me first to blank view and then to my selected <code>cell</code>. If I select another <code>cell</code>, number 3 on the <code>indexPath</code> the blank view is now the first <code>cell</code> from my previous choice after which it takes to my selected third <code>cell</code> . Its doing that every time. If I remove the <code>performSegueWithIdentifer</code> code (from Xcode 6.2 (in 6.1.1 it was random)) the selection is my previous choice and never my 'selectedCell', but then at least its only selecting once instead of twice to get to a <code>view</code>. There is something going wrong on the <code>indexPath</code>. This is the code for my <code>prepareForSegue</code></p>
  
  <pre><code>override func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?) {
        if segue.identifer == "showDetail" {
            let detailVC:DetailViewController = segue.destinationViewController as DetailViewController
               detailVC.selectedImageName = selectedImage
               detailVC.selectedLabel = selectedLabels
               }
        }
  </code></pre>
  
  <p>I'm stuck on what to do &amp; what solution to apply. Do I keep <code>performSegueWithIdentifer</code> code &amp; create an <code>Equatable</code> to implement <code>find(array, selection)</code> on the <code>indexPath</code>? Or could I write a loop, (which seems much easier), that would run through the <code>indexPath</code> based upon the selections and that would remove the cell that is no longer selected. However I'm not sure what condition to write in the loop because I don't know the value of the property of the 'selectedCell' because its optional. </p>
  
  <pre><code>for (index, value) in enumerate(cellItems) {
  //something here to remove 'selectedItem' in the indexPath
  }
  </code></pre>
  
  <p>If I remove <code>performSegueWithIdentifer</code> code from <code>didSelectItemAtIndexPath</code> what can I do in my <code>prepareForSegue</code> to get the selection on the correct indexPath? </p>
  
  <p>EDIT the complete code at <code>didSelectItemAtIndexPath</code> </p>
  
  <pre><code>override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
       selectedImage = cellImages[indexPath.row] as String
       selectedLabels = cellLabels[indexPath.row] as String
       self.performSegueWithIdentifer("showDetail", sender: self)
     }
  </code></pre>
  
  <p>I've tried changing sender in the <code>performSegueWithIdentifer</code> to <code>indexPath</code> but the problem still remains. </p>
  
  <p>EDIT 2 Complete code to my CollectionViewController</p>
  
  <pre><code>class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  
  @IBOutlet weak var collectionView: UICollectionView!
  
  var selectedImage = String()
  var selectedLabels = String()
  
  var cellImages:[String] = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpg", "13.jpg", "14jpg"]
  
  var cellLabels:[String] = ["Photo 1", "Photo 2", "Photo 3", "Photo 4", "Photo 5", "Photo 6", "Photo 7", "Photo 8", "Photo 9", "Photo 10", "Photo 11", "Photo 12", "Photo 13", "Photo 14"]
  
  func collectionView(collectionView: UICollectionView, numberOfNumberItemsInSection: Int) -&gt; Int {
      return cellImages.count
  }
  
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
      let cell: PhotoViewCell = collectionView.dequeueReuseableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as PhotoViewCell 
      cell.labelCell.text = cellLabels[indexPath.row]
      cell.ImageCell.image = UIImage(named: cellImages[indexPath.row])
      return cell
  }
  
   override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
       selectedImage = cellImages[indexPath.row] as String
       selectedLabels = cellLabels[indexPath.row] as String
       self.performSegueWithIdentifer("showDetail", sender: self)
     }
  
  override func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?) {
        if segue.identifer == "showDetail" {
            let detailVC:DetailViewController = segue.destinationViewController as DetailViewController
            detailVC.selectedImageName = selectedImage
            detailVC.selectedLabel = selectedLabels
            }
        }
  }
  </code></pre>
  
  <p>PhotoViewCell</p>
  
  <pre><code>class PhotoViewCell: UICollectionViewCell {
  
  @IBOutlet var labelCell: UILabel!
  @IBOutlet var ImageCell: UIImage!
  
  }
  </code></pre>
  
  <p>EDIT 3 - Amended </p>
  
  <p>I tried your suggestion and unfortunately the problem is still persisting on double views - it's still passing two views before it takes me to the actual selected <code>cell</code>. I also amended the code slightly in the <code>didSelectItemAtIndexPath</code> but it still didn't fix the problem.  </p>
  
  <pre><code>if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? PhotoViewCell {
  
  performSegueWithIdentifier("showDetail", sender: cell) 
  }
  </code></pre>
  
  <p>However following your other suggestion, in my <code>StoryBoard</code> I have added a <code>segue</code> from my <code>Collection View</code> <code>cell</code> to my <code>DetailViewController</code>, which has the identifier "showDetail". If I remove <code>segue</code> nothing can be selected from my <code>cells</code>. </p>
  
  <p>Although it seems the <code>performSegueWithIdentifer</code> code is the trigger for the double views because when I remove it, the <code>cell</code> is only being selected once, the problem was that the <code>indexPath</code> of the <code>cell</code> selection was not correct, because it's first selecting on a blank view (is that to do with the showDetail <code>segue</code>?), which then puts my <code>indexPath</code> out of sync.   </p>
  
  <p>EDIT - Solved </p>
  
  <p>This stopped the double selections (the <code>performSegueWithIdentifier</code> line was removed): -</p>
  
  <pre><code>override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
  if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
  cellLabels[indexPath.row] as String
  cellImages[indexPath.row] as String
     }
  }
  </code></pre>
  
  <p>Many Thanks for your help !!!! </p>