【问题标题】:How to cache UIViewControllers for UICollectionViewCell use?如何缓存 UIViewControllers 以供 UICollectionViewCell 使用?
【发布时间】:2019-09-18 07:17:57
【问题描述】:

我创建了一个UICollectionView,其中的单元格包含几种类型的UIViewController 视图作为它们的contentView。我想缓存未使用的UIViewController。我已经尝试根据他们的 indexPath 创建当前使用的UIViewController 的字典,以及一组未使用的UIViewController 要回收UIViewController 的字典。但是我注意到我的 UIViewControllers 没有被取消初始化。我的实现基于这些答案Add UIViewController to UICollectionViewCellMake PageViewController reuse viewControllers like tableView

下面是我的实现

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        addViewController(toCell: cell, at: indexPath, of: viewTypes[indexPath.item])
    }

    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        removeViewController(fromCell: cell, at: indexPath)
     //   cleanCachedViewControllers(index: indexPath.item)
    }

    func addViewController(toCell cell:UICollectionViewCell, at indexPath:IndexPath,of type:ViewControllerType){
        var childViewController:UIViewController!

        if let cvc = currentViewControllers[indexPath]{
            childViewController = cvc
        }else{
            childViewController = TestViewController()
            currentViewControllers[indexPath] = childViewController
        }
        self.addChild(childViewController)
        childViewController.view.frame = cell.contentView.bounds
        cell.contentView.addSubview(childViewController.view)
        childViewController.didMove(toParent: self)
    }

    func removeViewController(fromCell cell:UICollectionViewCell, at indexPath:IndexPath){
        guard let childViewController = currentViewControllers[indexPath] else {
            return
        }
        print("remove view controller called")
        childViewController.willMove(toParent: nil)
        childViewController.view.removeFromSuperview()
        childViewController.removeFromParent()
    //        currentViewControllers[indexPath] = nil
   //         currentViewControllers.removeValue(forKey: indexPath)
        childViewController.view = nil
        recycledViewControllers.insert(childViewController)
    } 

    func getRecycledViewController(type:ViewControllerType)->UIViewController{

        var unusedViewControllers = recycledViewControllers.filter { (viewController) -> Bool in
            return viewController.isKind(of: type.type) && viewController.parent == nil
        }

        if let reusableViewController = unusedViewControllers.first{
            recycledViewControllers.remove(reusableViewController)
            return reusableViewController
        }else{
            let newViewController = type.initializeViewController()
            //            recycledViewControllers.append(newViewController)
            return newViewController
        }
    }

【问题讨论】:

    标签: ios swift caching uiviewcontroller uicollectionview


    【解决方案1】:

    你可能有一个循环引用阻止你的对象被释放。

    Xcode 8 及更高版本允许您调试和可视化当前内存图。这对于排除对象未被释放的原因非常有用。

    https://developer.apple.com/videos/play/wwdc2018/416/

    【讨论】:

      猜你喜欢
      • 2015-08-03
      • 2010-10-29
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      相关资源
      最近更新 更多