title: UICollectionView: Random Cell highlighted
body: <p>I have a collectionview with various cells. One can be selected at once and the selection state is stored inside my business logic. If a cell is selected the whole section get's reloaded to update the UI to highlight the currently selected cell.</p>
  
  <pre><code>  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      ...
      cell.setItemSelected(selected: selected)
      return cell
      }
  </code></pre>
  
  <p>My problem is that a random cell get's highlighted to for a short time.
  I also implemented <code>prepareForReuse()</code> but without any effect. </p>
  
  <p>What can cause this?</p>
  
  <p>Thanks!</p>
  
  <p>Here the requested additional code:</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
          let pendencyDeficiencyType = deficiencyTypes[indexPath.row]
  
          if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PendencyDetailCell.reuseIdentifier, for: indexPath) as? PendencyDetailCell{
              cell.titleLabel.attributedText = StringUtils.hyphenedText(pendencyDeficiencyType.rawValue)
              cell.indexPath = indexPath
              cell.delegate = self
  
              let selected = pendencyDeficiencyType == pendencyItem?.pendencyDeficiencyType
              cell.setItemSelected(selected: selected)
  
              return cell
          }
          return collectionView.dequeueReusableCell(withReuseIdentifier: PendencyDetailCell.reuseIdentifier, for: indexPath)
      }
  
  override func prepareForReuse() {
      super.prepareForReuse()
      backgroundColor = Styling.cellDefaultColor
      titleLabel.textColor = .black
  }
  
  func setItemSelected(selected: Bool){
          layer.borderColor = selected ? Styling.heagPrimary.cgColor :  UIColor.darkGray.cgColor
          backgroundColor = selected ? Styling.cellSelectedColor : Styling.cellDefaultColor
          titleLabel.textColor = selected ? .white : .black
      }
  </code></pre>