Here's one fairly simple approach:
In your view controller, define a property to hold any array of cell reuse identifiers that can be referenced later. You can set an initial, non-random configuration, like this:
var reuseIdentifiers = ["GameOver", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal"]
Define the following method, and call it sometime during the initialization of your view controller, before the collectionView is shown on screen, and before each time the collection view data is reloaded:
func randomizeReuseIdentifiers() {
var randomized = [String]()
for _ in initialIdentifiers {
let random = reuseIdentifiers.removeAtIndex(Int(arc4random_uniform(UInt32(reuseIdentifiers.count))))
randomized.append(random)
}
reuseIdentifiers = randomized
}
And then in your collectionView(cellForItemAtIndexPath:NSIndexPath)
method, look up the matching reuseIdentifier for the current indexPath, like this:
func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let identifier = reuseIdentifiers[indexPath.item]
return collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath)
}
This approach also has the benefit of allowing you to add additional identifiers into the initial reuseIdentifiers
array in the future, to include other types of cells in one or more random locations.