我想放一个UICollectionView
在每个可重复使用的UICollectionViewCell
. The 灰犁沟方法对我来说效果不太好,因为使用一种UICollectionViewController
两个数据源和委托的类UICollectionViews
不起作用。
我最新的方法是将观点UICollectionViewController
在每个单元格内,如中所述这个问题 and this doc。但是,当我尝试加载视图时,我的应用程序冻结了。在 Xcode 调试导航器中,CPU 恒定为 107%,几秒钟后内存接近 1GB。
在这个例子中,我试图得到一个ThumbnailCollectionViewController
每一个MainCollectionViewControllerCell
。每个人中唯一的一件事ThumbnailCollectionViewControllerCell
是尺寸为 50x50 的图像。
如何正确完成此操作?
在 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)
}
缩略图 uiCollectionViewController
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
}