title: Swift iOS -Change background color of old cell when a new cell is selected
body: <p>Before I begin it should be noted that if I don't change months or better yet after I manually select a cell, <code>didDeselectItem</code> does work fine.</p>
  
  <p>I have a collectionView with tableData of dates (1-31, 1-30, or 1-28). When a user selects a day the background color of that cell changes to red. If the user goes to a different month the tableData is reset and cellForItem is fired again so all the cells background colors are set to UIColor.clear.</p>
  
  <p>What I did to keep track of the initial selected day and it's month/year was create 3 variables that get initialized when <code>didSelectItem</code> is fired. When the user goes back to that month in <code>cellForItem</code> I check to see if the month and year match and if so change the background for that day. It works fine.</p>
  
  <p>The problem is if I choose a different day the background color of that does change to red but the initial selected day also stays red. Because a cell was never actually selected (I just changed the color in cellForItem) didDeselectItem never fires to change the background color of the old cell.</p>
  
  <p><strong>How can I get the initial day to change it's color when that new day is selected?</strong></p>
  
  <pre><code>// when a different month is shown some calculations are done to change these but those calculations aren't relevant to the question
  var currentMonthIndex = Calendar.current.component(.month, from: Date())
  var currentYear = Calendar.current.component(.year, from: Date())
  
  var selectedMonth = 0
  var selectedYear = 0
  var selectedDay = 0
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dayCell", for: indexPath) as! DayCell
  
      // clears the background color of all the cells when a new month is shown 
      cell.backgroundColor = UIColor..clear
  
      if selectedMonth == currentMonthIndex &amp;&amp; selectedYear == currentYear {
  
          // when coming back to the selected day/month/year this makes sure the selectedDay's background is still red 
          if indexPath.item == selectedDay {
              cell.backgroundColor = .red
          }
      }
  
      return cell
  }
  
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  
      guard let cell = collectionView.cellForItem(at: indexPath) as? DayCell else { return }
  
      cell.backgroundColor = .red
  
      selectedMonth = currentMonthIndex
      selectedDay = indexPath.item
      selectedYear = currentYear
  }
  
  func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
  
      guard let cell = collectionView.cellForItem(at: indexPath) as? DayCell else { return }
  
      cell.backgroundColor = UIColor.clear
  }
  </code></pre>
  
  <p>Here's an example if my question is unclear</p>
  
  <p>A user chooses 1/1/2019 and the background of that day is now red. If the user goes to 2/2019 the tableData will have to change because there are only 28 days so all the cells for that month are clear. If they scroll back to 1/2019 the tableData changes again because there are now 31 days in the month so all the cells background color are changed to clear again. However since the month and year of 1/2019 matches what was initially selected, in <code>cellForItem</code> I can now automatically change the background color of 1/1 to red again. The thing is if I select 1/10 it's background does change to red but <strong>1/1 still stays red</strong>. I need 1/1 to change to clear once I select 1/10.</p>