簡體   English   中英

如何緩存UIViewControllers用於UICollectionViewCell?

[英]How to cache UIViewControllers for UICollectionViewCell use?

我創建了一個UICollectionView ,其中包含幾種類型的UIViewController視圖作為其contentView。 我想緩存未使用的UIViewController 我已經嘗試根據他們的indexPath創建一個當前使用的UIViewController的字典,以及一組未使用的UIViewController來回收UIViewController的。 我注意到我的UIViewControllers沒有被取消初始化。 我的實現基於這些答案將UIViewController添加到UICollectionViewCell並使PageViewController重用viewControllers,如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
        }
    }

您可能有一個循環引用,可以防止對象解除分配。

Xcode 8及更高版本允許您調試和可視化當前內存圖。 這對於解決未釋放對象的原因非常有用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM