title: Swift 3 Xcode 8 Collection View Reload Data with Segmented Control
body: <p>I'm displaying 2 sets of data from 2 different arrays based on the segmented control selected index. Everything works fine, all the correct data loads up. However, one thing very strange happens:</p>
  
  <p>When I press the other segmented control 'button', the collection view is supposed to switch pretty smoothly (implemented reloadData() in the segmented control IBAction). But it flashes very quickly before the 'correct data' is loaded up. I've tried restarting Xcode and even gave it to a friend to test however, the quick 'flash' remains. It also happens when I go back to segmented control 'button1' from segmented control 'button 2'. </p>
  
  <p>When I look closely at what flashes, it turns out that the collection view displays data from my array in a random order (or so it appears to be) before quickly (very quickly!) switching back to its 'correct' order.</p>
  
  <p>Here is my view controller:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view.
  
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  var array1 = ["Red", "Yellow", "Purple"]
  var array2 = ["Apple", "Banana", "Grape"]
  
  @IBAction func segmentedControl(_ sender: UISegmentedControl) {
      collectionView.reloadData()
      // Reload Data
      collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems)
      // Prevents 'flash' from .reloadData()
  }
  
  @IBOutlet weak var segmentedOutlet: UISegmentedControl!
  // Creates an outlet for segmented control (that can be set)
  
  @IBOutlet weak var collectionView: UICollectionView!
  
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      switch segmentedOutlet.selectedSegmentIndex {
      case 0 : return array1.count
      case 1 : return array2.count
      default: return 0
      }
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! collectionViewCell
  
      switch segmentedOutlet.selectedSegmentIndex {
      case 0 : cell.button.setTitle(array1[indexPath.row], for: .normal)
      case 1 : cell.button.setTitle(array2[indexPath.row], for: .normal)
      default : break
      }
      return cell
  }
  </code></pre>