title: Changing content of UICollectionView with segmented Control in the header in Swift
body: <p>I tried to solve this problem since hours and didn't find a proper solution. I want to have a custom UICollectionView with a segmented control in the header. Changing the segmented control index should render the cells differently. So far I can display the segmented control in the header of my collectionView but changing the content of the collectionView inside of my UserSearchHeader doesn't work.</p>
  
  <p>I created a custom UICollectionView called <strong>UserSearchController</strong> which is a Subclass of UICollectionView and conforms the UICollectionViewDelegateFlowLayout protocol. </p>
  
  <pre><code>        class UserSearchController: UICollectionViewController, 
                UICollectionViewDelegateFlowLayout, UISearchBarDelegate { ....
  </code></pre>
  
  <p>My custom collectionView <strong>UserSearchController</strong> has a custom header (<strong>UserSearchHeader</strong>) and custom cells (<strong>UserSearchCell</strong>).   </p>
  
  <pre><code>  collectionView?.register(UserSearchCell.self, forCellWithReuseIdentifier: cellId)
    collectionView?.register(UserSearchHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerId")
  </code></pre>
  
  <p>The <strong>UserSearchHeader</strong> contains the segmented control. </p>
  
  <pre><code>   class UserSearchHeader: UICollectionViewCell {
  
  var segmentedControl: UISegmentedControl = {
      let sc = UISegmentedControl(items: ["Username", "Hashtag"])
      sc.translatesAutoresizingMaskIntoConstraints = false
      sc.tintColor = UIColor.black
      sc.selectedSegmentIndex = 0 // automatically highlight
      //  sc.addTarget(self, action: #selector(segmentedChange), for: .valueChanged)
      return sc
  }() .....
  </code></pre>
  
  <p>If the segmentedIndex is 0 I want to render the cells with the UserSearchCell class if the segmentedIndex is 1 I want to render it with a different cellClass (Clicking the segmented control). How can I achieve this behavior with these different classes. shall I use the cellForItem at method inside of the UserSearchController to check the status of the segmented control. But how can I do this when the segmented control is defined in the UserSearchHeader class . </p>
  
  <pre><code>       override func collectionView(_ collectionView: 
      UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; 
    UICollect. ionViewCell { if segmentedControl.segmentedIndex = 0 ....}
  </code></pre>