title: UICollectionview Delegate and datasource not begin called
body: <p>I'm trying to create UICollectionview programmatically,</p>
  
  <p>but for some reason the delegates methods not begin called.
  I can see in view debugger that the delegate and datasource is nil 
  what can be the problem?
  thanks</p>
  
  <p><a href="https://i.stack.imgur.com/ERuwo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ERuwo.png" alt="enter image description here"></a></p>
  
  <pre><code>    class MyClass: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  
               var contentView : UIView
               var collectionView : UICollectionView
               var images = [UIImage]()
              init(contentView : UIView, images : [UIImage]) {
  
                  self.contentView = contentView
                  self.images = images 
  
                  let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
                  layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                  layout.itemSize = CGSize(width: contentView.frame.width, height: 700)
                  layout.scrollDirection = .horizontal
  
                  let cv = UICollectionView(frame: contentView.frame, collectionViewLayout: layout)
  
                  cv.backgroundColor = .clear
                  let nib = UINib(nibName: "MyCell", bundle: .main)
                  cv.register(nib, forCellWithReuseIdentifier: "MyCell")
                  self.collectionView = cv
  
                  super.init()
  
                  self.collectionView.delegate = self
                  self.collectionView.dataSource = self
  
                  contentView.addSubview(cv)
  
  
              }
      }
  </code></pre>
  
  <p>Then I instantiate <code>MyClass</code> from ViewController :</p>
  
  <pre><code>class SecondViewController: UIViewController {
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let images : [UIImage] = [...]
          let cv = MyClass(contentView: self.view, images: images)
  
          cv.startAnimation()
  
  
      }
  } 
  </code></pre>