title: Auto Select Middle Visible Cell Of Collection View
body: <p>I'm trying to select and highlight the middle cell of the visible cells in a collection view at any given time. The collection view in question displays days for six months forwards and back.</p>
  
  <p>I've tried using the scroll view delegates and the collection view delegates. But all that works is select and highlight code in <code>didSelectItem()</code> collection view delegate.</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  
      print("delegate called")
      collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
      collectionView.cellForItem(at: indexPath)?.backgroundColor = UIColor.highlightCellGreen()
      if let cell = collectionView.cellForItem(at: indexPath) as? ClientListDateCollectionViewCell{
          monthLabel.text = cell.monthName
          monthLabel.text = monthLabel.text?.capitalized
  
  }                                                                                      
  </code></pre>
  
  <p>I tried to select the middle cell while scrolling using the <code>viewDidScroll()</code> delegate. But, I wasn't able to get the output I wanted.</p>
  
  <pre><code>func scrollViewDidScroll(_ scrollView: UIScrollView) {
  
         let visibleCellCount = dateCollectionView.indexPathsForVisibleItems.count
         let cellCount = dateCollectionView.visibleCells.count
  let visibleCells = dateCollectionView.indexPathsForVisibleItems[visibleCellCount-1/2]
        if visibleCellCount&gt;0{
         let middle = visibleCellCount/2
         let midValue = dateCollectionView.indexPathsForVisibleItems[middle]
             dateCollectionView.selectItem(at: midValue, animated: true, scrollPosition: .centeredHorizontally)
  }                                                                          
  </code></pre>
  
  <p>How do I go about selecting the middle cell?</p>
  
  <p>edit 1: The collection view starts on the leftmost point and then scrolls to the middle i.e, today's date</p>