Last active
January 25, 2016 05:11
-
-
Save ajfigueroa/502ed023984e6c7c79cb to your computer and use it in GitHub Desktop.
SO Question 34980383: http://stackoverflow.com/questions/34980383/could-not-dequeue-a-view-of-kind-uicollectionelementkindcell-with-identifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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