title: UICollectionView reloadSection does not remove old views from view hierarchy
body: <p><code>UICollectionView</code> does not remove the reusable view from view hierarchy when reloading section. This creates a problem for <code>UIAutomation</code>.</p>
  
  <p>I have created a simple application which uses <code>UICollectionView</code> and shows header, footer and cells. On tapping "<code>Reload</code>", <code>collectionView</code> displays different number of cells. Please refer below image.</p>
  
  <p><a href="https://i.stack.imgur.com/IkdKt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IkdKt.png" alt="enter image description here"></a></p>
  
  <p>On launching the app, it shows 2 cells. Tapping "<code>Reload</code>" first time shows <code>8 cells</code> and Tapping "<code>Reload</code>" second time shows <code>18 cells</code>. Tapping "Reload" third time show 2 cells. Now, at this moment if we debug the view using <code>Xcode View debugger</code>, it shows multiple cells in view hierarchy when disabling "Show only displayed Views". Refer below images:</p>
  
  <p><a href="https://i.stack.imgur.com/g4Bw2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g4Bw2.png" alt="&quot;Show only displayed Views&quot; option enabled"></a><a href="https://i.stack.imgur.com/qX2aN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qX2aN.png" alt="&quot;Show only displayed Views&quot; option disabled"></a></p>
  
  <p>And this is creating problems in <code>UIAutomation</code> in getting elements. I am not sure what is wrong in my implementation. </p>
  
  <p>Below is my Controller code:</p>
  
  <pre><code>struct CellData {
      var title: String
      var value: String
  }
  
  class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  
      @IBOutlet var collectionView: UICollectionView!
      private var data: [CellData]!
      private var data1: [CellData]!
      private var data2: [CellData]!
      private var data3: [CellData]!
      private var reloadIndex = 1
      private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
  
          self.data1 = [
              CellData(title: "Cell Title 1", value: "Value 1"),
              CellData(title: "Cell Title 2", value: "Value 2")]
          self.data = self.data1
  
          self.data2 = [
              CellData(title: "Cell Title 1", value: "Value 1"),
              CellData(title: "Cell Title 2", value: "Value 2"),
              CellData(title: "Cell Title 3", value: "Value 3"),
              CellData(title: "Cell Title 4", value: "Value 4"),
              CellData(title: "Cell Title 5", value: "Value 5"),
              CellData(title: "Cell Title 6", value: "Value 6"),
              CellData(title: "Cell Title 7", value: "Value 7"),
              CellData(title: "Cell Title 8", value: "Value 8")]
  
          self.data3 = [
              CellData(title: "Cell Title 1", value: "Value 1"),
              CellData(title: "Cell Title 2", value: "Value 2"),
              CellData(title: "Cell Title 3", value: "Value 3"),
              CellData(title: "Cell Title 4", value: "Value 4"),
              CellData(title: "Cell Title 5", value: "Value 5"),
              CellData(title: "Cell Title 6", value: "Value 6"),
              CellData(title: "Cell Title 7", value: "Value 7"),
              CellData(title: "Cell Title 8", value: "Value 8"),
              CellData(title: "Cell Title 9", value: "Value 9"),
              CellData(title: "Cell Title 10", value: "Value 10"),
              CellData(title: "Cell Title 11", value: "Value 11"),
              CellData(title: "Cell Title 12", value: "Value 12"),
              CellData(title: "Cell Title 13", value: "Value 13"),
              CellData(title: "Cell Title 14", value: "Value 14"),
              CellData(title: "Cell Title 15", value: "Value 15"),
              CellData(title: "Cell Title 16", value: "Value 16"),
              CellData(title: "Cell Title 17", value: "Value 17"),
              CellData(title: "Cell Title 18", value: "Value 18")]
  
          self.collectionView.registerNib(UINib(nibName: "CollectionCell", bundle: nil), forCellWithReuseIdentifier: "CollectionCell")
  
          self.collectionView.registerNib(UINib(nibName: "SectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeader")
  
          self.collectionView.registerNib(UINib(nibName: "SectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "SectionFooter")
      }
  
      @IBAction func reload() {
          self.activityIndicator.center = self.view.center
          self.view.addSubview(self.activityIndicator)
          self.activityIndicator.startAnimating()
          self.view.userInteractionEnabled = false
  
          let waitTime = 2 * Double(NSEC_PER_SEC)
          let dispatchTime = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(waitTime))
          dispatch_after(dispatchTime, dispatch_get_main_queue()) {
              self.reloadIndex += 1
  
              if self.reloadIndex &gt; 3 {
                  self.reloadIndex = 1
              }
              switch self.reloadIndex {
              case 2:
                  self.data = self.data2
                  break
              case 3:
                  self.data = self.data3
                  break
              default:
                  self.data = self.data1
              }
              self.collectionView.reloadSections(NSIndexSet(index: 0))
              //self.collectionView.reloadData()
  
              self.activityIndicator.stopAnimating()
              self.activityIndicator.removeFromSuperview()
              self.view.userInteractionEnabled = true
          }
      }
  
      @IBAction func print() {
          printViewHierarchyInJSON(self.view)
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return self.data.count
      }
  
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
  
          let compatibleCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath)
          (compatibleCell as? CollectionCell)?.setData(self.data[indexPath.item])
          return compatibleCell
      }
  
      func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -&gt; UICollectionReusableView {
  
          var supplementaryView = UICollectionReusableView()
  
          switch kind {
          case UICollectionElementKindSectionHeader:
              supplementaryView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeader", forIndexPath: indexPath)
  
          case UICollectionElementKindSectionFooter:
              supplementaryView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "SectionFooter", forIndexPath: indexPath)
  
          default:
              break
          }
  
          return supplementaryView
      }
  
  
      func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -&gt; CGSize {
          return CGSizeMake(collectionView.bounds.size.width, 50)
      }
  
      func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -&gt; CGSize {
          return CGSizeMake(collectionView.bounds.size.width, 104)
      }
  
      func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -&gt; CGSize {
          return CGSizeMake(collectionView.bounds.size.width, 86)
      }
  }
  </code></pre>
  
  <p>I have uploaded this sample app on <a href="https://www.dropbox.com/s/gp20x3zz15f0vqx/CollectionViewDemo.zip?dl=0" rel="nofollow noreferrer">dropbox</a>. </p>
  
  <p>Thank you in advance.</p>