UICollectionViewCell(Swift)中的UICollectionView [英] UICollectionView within a UICollectionViewCell (Swift)

查看:89
本文介绍了UICollectionViewCell(Swift)中的UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在每个可重复使用的 UICollectionViewCell 中放置一个 UICollectionView Ash Furrow 方法效果不佳对我来说,因为为数据源使用一个 UICollectionViewController 类,并且两个 UICollectionViews 的委托不起作用。

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 的视图,如这个问题此文档。但是,当我尝试加载视图时,我的应用程序冻结了。在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 中的code> 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 UIImageViews being added to each thumbnail cell.

这篇关于UICollectionViewCell(Swift)中的UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆