简体   繁体   English

UICollectionViewCell(Swift)中的UICollectionView

[英]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.

相关问题 iOS(Swift):给定UICollectionViewCell的UICollectionView - iOS (Swift): UICollectionView for a given UICollectionViewCell 当UICollectionview在Swift 3中滚动时,如何禁用UICollectionViewCell - How to disable UICollectionViewCell when UICollectionview is scrolling in swift 3 获取UICollectionView Swift中单击的UICollectionViewCell的索引 - Get index of clicked UICollectionViewCell in UICollectionView Swift 如何使父uicollectionview的uicollectionviewcell中的uicollectionview的布局无效? - How to invalidate layout of a uicollectionview within the uicollectionviewcell of a parent uicollectionview? UICollectionViewCell 中的 UICollectionView - UICollectionView in a UICollectionViewCell Swift-如何根据整数中的索引位置从UICollectionView中获取UICollectionViewCell - Swift - How to fetch a UICollectionViewCell from UICollectionView based on index position in integer UIcollectionViewCell中的UITextField的单个边框(swift 3 xcode) - single border for UITextField within a UIcollectionViewCell (swift 3 xcode) UICollectionView和选定的UICollectionViewCell - UICollectionView and selected UICollectionViewCell 将UICollectionViewCell放入UICollectionView - Put UICollectionViewCell into UICollectionView 在UICollectionViewCell中实现UICollectionView - Implement UICollectionView in a UICollectionViewCell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM