I'm trying to put a UICollectionView
within each reusable UICollectionViewCell
. The Ash Furrow method didn't work out too well for me, because using one UICollectionViewController
class for the data source and delegate of two UICollectionViews
does not work.
My latest approach is to put the view of a UICollectionViewController
inside each cell, as described in this question and this doc. However, when I go try to load the view, my app freezes. In the Xcode debug navigator, the CPU is at a constant 107% and the Memory approaches 1GB after a few seconds.
In this example, I am trying to get a ThumbnailCollectionViewController
in each MainCollectionViewControllerCell
. The only thing in each ThumbnailCollectionViewControllerCell
is an image of size 50x50.
How is this done properly?
In MainCollectionViewController
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
let thumbsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ThumbnailColllection") as! ThumbnailCollectionViewController
self.addChildViewController(thumbsViewController)
thumbsViewController.view.frame = cell.bounds
cell.addSubview(thumbsViewController.view)
thumbsViewController.didMoveToParentViewController(self)
}
ThumbnailCollectionViewController
let reuseIdentifier = "ThumbCell"
let thumbnails = ["red", "green", "blue"]
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return thumbnails.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
let cellImage = UIImage(named: thumbnails[indexPath.row])
let cellImageView = UIImageView(image: cellImage)
cellImageView.frame = cell.bounds
cell.addSubview(cellImageView)
return cell
}