collectionView: reloadData works, reloadItemsAtIndexPaths does not

Swift 4 reload all items in section

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    var indexPaths: [NSIndexPath] = []
    for i in 0..<collectionView!.numberOfItems(inSection: 0) {
        indexPaths.append(NSIndexPath(item: i, section: 0))
    }
    collectionView?.reloadItems(at: indexPaths as [IndexPath])
}

From your code it looks like you have declared your collectionView as Optional (with ?) at the end and maybe linked it to your storyboard using an @IBOutlet. To fix your issue you should remove the ? from the :

@IBOutlet var collectionView: UICollectionView?

and substitute it with:

@IBOutlet var collectionView: UICollectionView!

this way you are telling the compiler that your collectionView definitely exists (because you have linked it from your storyboard).

Alternatively, you can bind your collectionView by doing this:

if let collectionView = collectionView {
    collectionView.reloadItemsAtIndexPaths(myArrayOfIndexPaths)
}