【发布时间】:2015-08-12 05:10:40
【问题描述】:
我试图在每个可重复使用的UICollectionViewCell 中添加一个UICollectionView。 Ash Furrow 方法对我来说效果不太好,因为使用一个 UICollectionViewController 类作为数据源和两个 UICollectionViews 的委托是行不通的。
我的最新方法是将UICollectionViewController 的视图放入每个单元格中,如this question 和this doc 中所述。但是,当我尝试加载视图时,我的应用程序冻结了。在 Xcode 调试导航器中,CPU 保持在恒定的 107%,几秒钟后内存接近 1GB。
在这个例子中,我试图在每个MainCollectionViewControllerCell 中获得一个ThumbnailCollectionViewController。每个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)
}
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
}
【问题讨论】:
标签: ios iphone swift uicollectionview uicollectionviewcell