Skip to content

Instantly share code, notes, and snippets.

@ajfigueroa
Last active January 25, 2016 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajfigueroa/502ed023984e6c7c79cb to your computer and use it in GitHub Desktop.
Save ajfigueroa/502ed023984e6c7c79cb to your computer and use it in GitHub Desktop.
/**
Assume there are two collection views: collectionViewA and collectionViewB and both have the same dataSource (although, they probably shouldn't...)
collectionViewA has registered a UICollectionViewCell class with the reuseIdentifier "CellA".
collectionViewB has registered a UICollectionViewCell class with the reuseIdentifier "CellB".
When dequeueing a cell with a given reuseIdentifier, you should be confident that the collectionView has the reuseIdentifier
you're attempting to dequeue registered to it. That is, ONLY collectionViewA can dequeue a cell with reuseIdentifier "CellA" and ONLY collectionViewB can do the same for "CellB"
Otherwise, a crash will occur.
Notice, how one of the arguments is a collectionView in the UICollectionViewDataSource method below? That is the collectionView that is asking the datasource for a cell which
means it could be either collectionViewA or collectionViewB in this scenario.
Thus, you need to check which collecitonView it is before dequeueing as follows...
*/
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView == collectionViewA {
// At this point, collectionView is certain to be collectionViewA
let cellA = collectionView.dequeueReusableCellWithReuseIdentifier("CellA", forIndexPath: indexPath)
// Configure your cell here...
return cellA
}
else if collectionView == collectionViewB {
let cellB = collectionView.dequeueReusableCellWithReuseIdentifier("CellB", forIndexPath: indexPath)
// Configure your cell here...
return cellB
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment