[英]UICollectionView within a UICollectionViewCell (Swift)
I'm trying to put a UICollectionView
within each reusable UICollectionViewCell
. 我试图把UICollectionView
每个可重复使用的内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. Ash Furrow方法对我来说效果不好,因为对数据源使用一个UICollectionViewController
类并且两个UICollectionViews
委托不起作用。
My latest approach is to put the view of a UICollectionViewController
inside each cell, as described in this question and this doc . 我最新的方法是将UICollectionViewController
的视图放在每个单元格中,如本问题和本文档中所述 。 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. 在Xcode调试导航器中,CPU处于恒定的107%,几秒钟后内存接近1GB。
In this example, I am trying to get a ThumbnailCollectionViewController
in each MainCollectionViewControllerCell
. 在这个例子中,我试图在每个MainCollectionViewControllerCell
获得一个ThumbnailCollectionViewController
。 The only thing in each ThumbnailCollectionViewControllerCell
is an image of size 50x50. 每个ThumbnailCollectionViewControllerCell
唯一的东西是大小为50x50的图像。
How is this done properly? 这怎么做得好?
In MainCollectionViewController 在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 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
}
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. 您在每个单元格中放置UICollectionView
方法看起来很好,但您很可能会看到非常糟糕的性能,因为您错误地处理了单元格重用。 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?
}
Back in your MainCollectionViewController: 回到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
}
}
And again, something similar to prevent multiple UIImageView
s being added to each thumbnail cell. 同样,类似的东西阻止多个UIImageView
被添加到每个缩略图单元格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.