//
//  ContentCollectionViewCell.swift
//  ICViewPager
//
//  Created by Ilter Cengiz on 18/5/18.
//  Copyright © 2018 Ilter Cengiz. All rights reserved.
//

import UIKit

final class ContentCollectionViewCell: UICollectionViewCell {
    
    private var contentViewController: UIViewController?
    
    // MARK: Public functions
    
    func configure(contentViewController: UIViewController) {
        self.contentViewController = contentViewController
    }
    
    // MARK: View controller containment
    
    func addContentViewController(to parentViewController: UIViewController) {
        
        guard let viewController = contentViewController else { return }
        
        parentViewController.addChildViewController(viewController)
        contentView.embed(viewController.view)
        viewController.didMove(toParentViewController: parentViewController)
        contentViewController = viewController
    }
    
    func removeContentViewController() {
        contentViewController?.willMove(toParentViewController: nil)
        contentViewController?.view.removeFromSuperview()
        contentViewController?.removeFromParentViewController()
    }
}

//
//  ContentCollectionViewCell.swift
//  ICViewPager
//
//  Created by Ilter Cengiz on 18/5/18.
//  Copyright © 2018 Ilter Cengiz. All rights reserved.
//

import UIKit

final class ContentCollectionViewCell: UICollectionViewCell {
    
    private weak var contentViewController: UIViewController?
    
    // MARK: Public functions
    
    func configure(contentViewController: UIViewController) {
        self.contentViewController = contentViewController
    }
    
    // MARK: View controller containment
    
    func addContentViewController(to parentViewController: UIViewController) {
        
        guard let viewController = contentViewController else { return }
        
        parentViewController.addChildViewController(viewController)
        contentView.embed(viewController.view)
        viewController.didMove(toParentViewController: parentViewController)
        contentViewController = viewController
    }
    
    func removeContentViewController() {
        contentViewController?.willMove(toParentViewController: nil)
        contentViewController?.view.removeFromSuperview()
        contentViewController?.removeFromParentViewController()
        contentViewController = nil
    }
}

--- 
+++ 
@@ -10,7 +10,7 @@
 
 final class ContentCollectionViewCell: UICollectionViewCell {
     
-    private weak var contentViewController: UIViewController?
+    private var contentViewController: UIViewController?
     
     // MARK: Public functions
     
@@ -34,6 +34,5 @@
         contentViewController?.willMove(toParentViewController: nil)
         contentViewController?.view.removeFromSuperview()
         contentViewController?.removeFromParentViewController()
-        contentViewController = nil
     }
 }
Fix for view controller embedding

I assumed keeping a weak reference to the content view controller would be fine, but turns out collection view might not call data source function cellForItemAt:IndexPath to let me configure the cell again with the view controller, because it’s already cached.
