UICollectionViewCell 中的 UICollectionView (Swift) [英] UICollectionView within a UICollectionViewCell (Swift)
问题描述
我试图在每个可重复使用的 UICollectionViewCell
中放置一个 UICollectionView
.Ash Furrow 方法效果不佳对我来说,因为对两个 UICollectionViews
的数据源和委托使用一个 UICollectionViewController
类不起作用.
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.
我的最新方法是将 UICollectionViewController
的视图放在每个单元格中,如 this question 中所述 和 此文档.但是,当我尝试加载视图时,我的应用程序冻结了.在 Xcode 调试导航器中,CPU 保持在恒定的 107% 并且内存在几秒钟后接近 1GB.
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.
在本例中,我试图在每个 MainCollectionViewControllerCell
中获取一个 ThumbnailCollectionViewController
.每个 ThumbnailCollectionViewControllerCell
中唯一的东西是大小为 50x50 的图像.
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.
这是如何正确完成的?
在 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
}
推荐答案
您在每个单元格中放置 UICollectionView
的方法看起来不错,但您很可能会看到糟糕的性能,因为您对单元格重用处理不当.每次请求新的可重用单元格时,您都在向单元格添加一个新的集合视图(实际上,每个缩略图单元格都有一个新的图像视图).如果您连续滚动,那么 已经 添加了这些子视图的单元格将出列,因此每个单元格下都会有许多子视图.
Your approach to putting a UICollectionView
inside each cell looks fine, but you are most likely seeing atrocious performance because you are mishandling cell reuse. You are adding a new collection view to the cell (and actually, a new image view to every thumbnail cell) EVERY time a new reusable cell is requested. If you scroll continuously, then cells will be dequeued that already have these subviews added, so you will end up with many many subviews under each cell.
相反,添加一种检查单元格是否已经具有子视图的方法,如果没有,则仅添加它.可能最好的方法是使用自定义单元子类:
Instead, add a means of checking whether a cell already has the subview or not, and only add it if it doesn't. Probably the best way is by using a custom cell subclass:
class ThumbnailCollectionCell: UICollectionViewCell {
var thumbnailViewController: ThumbnailCollectionViewController?
}
回到你的 MainCollectionViewController:
Back in your MainCollectionViewController:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ThumbnailCollectionCell
if cell.thumbnailViewController == nil {
let thumbsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ThumbnailColllection") as! ThumbnailCollectionViewController
self.addChildViewController(thumbsViewController)
thumbsViewController.view.frame = cell.bounds
cell.addSubview(thumbsViewController.view)
thumbsViewController.didMoveToParentViewController(self)
cell.thumbnailViewController = thumbsViewController
}
}
同样,类似的东西可以防止多个 UIImageView
被添加到每个缩略图单元格中.
And again, something similar to prevent multiple UIImageView
s being added to each thumbnail cell.
这篇关于UICollectionViewCell 中的 UICollectionView (Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!