title: Swift 3 selected button changing postition in collection view when scrolling
body: <p>I am trying to make a collection view where each cell contains an image and a button that can each either be selected or not selected. Initially everything appears to work well, however after I press some buttons and move through the view some 'selected' buttons change to 'not selected' and vice versa. </p>
  
  <p>I found that people have issues with images switching places so I have tried caching my images. I have also tried removing the images completely but the issue remains.</p>
  
  <p>Here is my collection view setup:</p>
  
  <pre><code>override func collectionView(_: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
  
      return imageArray.count
  
  }
  
  
  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
  
      //cell.photo.image = imageArray[indexPath.row]
  
      cell.selectButton.tag = indexPath.row
      cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)
  
      return cell
  
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
  
      let length = collectionView.frame.width / 3 - 1
  
  
      return CGSize(width: length, height: length)
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -&gt; CGFloat {
      //sets spacing between images
      return 1.0
  }
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -&gt; CGFloat {
      //sets spacing between images
      return 1.0
  }
  </code></pre>
  
  <p>Here is my buttonPressed function:</p>
  
  <pre><code>func buttonPressed(sender: UIButton) {
  
      if sender.isSelected == true{
          sender.setTitleColor(UIColor.blue, for:UIControlState.normal)
          sender.isSelected = false
      }else{
          sender.setTitleColor(UIColor.red, for:UIControlState.normal)
          sender.isSelected = true
      }
  }
  </code></pre>
  
  <p>How can this be solved?</p>