【问题标题】:UICollectionView within a UICollectionViewCell (Swift)UICollectionViewCell 中的 UICollectionView (Swift)
【发布时间】:2015-08-12 05:10:40
【问题描述】:

我试图在每个可重复使用的UICollectionViewCell 中添加一个UICollectionViewAsh Furrow 方法对我来说效果不太好,因为使用一个 UICollectionViewController 类作为数据源和两个 UICollectionViews 的委托是行不通的。

我的最新方法是将UICollectionViewController 的视图放入每个单元格中,如this questionthis 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


    【解决方案1】:

    您在每个单元格中放置 UICollectionView 的方法看起来不错,但您很可能会看到糟糕的性能,因为您处理错误的单元格重用。每次请求新的可重用单元格时,您都在向单元格添加一个新的集合视图(实际上,每个缩略图单元格都有一个新的图像视图)。如果您连续滚动,那么 已经 添加了这些子视图的单元格将出列,因此每个单元格下都会有许多子视图。

    相反,添加一种检查单元格是否已经具有子视图的方法,如果没有,则仅添加它。可能最好的方法是使用自定义单元子类:

    class ThumbnailCollectionCell: UICollectionViewCell {
        var thumbnailViewController: ThumbnailCollectionViewController?
    }
    

    回到你的 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
        }
    }
    

    同样,类似的东西可以防止多个UIImageViews 被添加到每个缩略图单元格中。

    【讨论】:

    • @Stuart,我无法添加“ThumbnailCollectionViewController”。你能帮忙吗?
    • @Stuart 在我的情况下,我想向不同的单元格添加不同的视图...例如,它可以是集合视图,或者运行时的图表或日历视图...所以这可以是对我来说最好的方法...嵌套集合视图或使用不同的选项卡...或带有单列和不同视图的表格视图
    猜你喜欢
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多