简体   繁体   English

如何缓存UIViewControllers用于UICollectionViewCell?

[英]How to cache UIViewControllers for UICollectionViewCell use?

I have created a UICollectionView with cells containing several types of UIViewController views as their contentView. 我创建了一个UICollectionView ,其中包含几种类型的UIViewController视图作为其contentView。 I would like to cache the UIViewController 's which are not in use. 我想缓存未使用的UIViewController I have tried creating a dictionary of the currently used UIViewController 's based on their indexPath, and a set of unused UIViewController 's to to be recycled UIViewController 's. 我已经尝试根据他们的indexPath创建一个当前使用的UIViewController的字典,以及一组未使用的UIViewController来回收UIViewController的。 Im noticing however that my UIViewControllers are not being deinitialized. 我注意到我的UIViewControllers没有被取消初始化。 I've based my implementation on these answers Add UIViewController to UICollectionViewCell and Make PageViewController reuse viewControllers like tableView . 我的实现基于这些答案将UIViewController添加到UICollectionViewCell并使PageViewController重用viewControllers,如tableView

Below is my implementation 以下是我的实施

    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
        }
    }

You probably have a circular reference that is keeping your objects from deallocating. 您可能有一个循环引用,可以防止对象解除分配。

Xcode 8 and higher allows you to debug and visualize the current memory graph. Xcode 8及更高版本允许您调试和可视化当前内存图。 This is very useful to troubleshoot why an object is not being deallocated. 这对于解决未释放对象的原因非常有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollectionView内部的UiviewControllers - UiviewControllers inside UICollectionViewCell 如何将AFNetworking与多个UIViewControllers一起使用? - How to use AFNetworking with multiple UIViewControllers? 如何在 UITableView 中使用 UICollectionViewCell? - How to use a UICollectionViewCell in a UITableView? 您可以缓存UIViewControllers吗? - Can you cache UIViewControllers? 如何对所有UIViewController使用相同的UINavigationBar - How to use same UINavigationBar for all UIViewControllers 如何在UICollectionViewCell上使用实时计时器? - How to use live timer on UICollectionViewCell? 如何在 UICollectionViewCell 中使用 WKWebView? - How to use WKWebView inside UICollectionViewCell? 如何在 Swift 中将 UILongPressGestureRecognizer 与 UICollectionViewCell 一起使用? - How do I use UILongPressGestureRecognizer with a UICollectionViewCell in Swift? 如何在自定义UICollectionViewCell和CollectionViewLayout中使用rbcollectionviewinfofolderlayout - How to use rbcollectionviewinfofolderlayout with custom UICollectionViewCell & CollectionViewLayout Swift:如何在UICollectionViewCell中使用“prepareForSegue”? - Swift: How to use “prepareForSegue” inside UICollectionViewCell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM