title: CollectionView + Page control
body: <p>I've collectionView where every cell is a page (the cell occupies the whole screen in portrait mode and 2 cells in landscape mode). I'm trying to add the current page number/the total number of pages (i.e 1/30) what I got so far 1/0 --&gt; one is the current page number and 0 is the total number of pages.</p>
  <p>there are two problems:</p>
  <ul>
  <li>the total number of pages show up only when I start scrolling</li>
  <li>when I scroll the current page number doesn't change.</li>
  </ul>
  <p>the code I use:</p>
  <pre><code>func scrollViewDidScroll(_ scrollView: UIScrollView) {
  
          if pageControl.currentPage &lt; pages.count {
                     pageControl.currentPage += 1
                      numberOfPagesLabel.text = &quot;\(pageControl.currentPage + 1)/\(pages.count)&quot;
          } else if pageControl.currentPage != 0 {
                      pageControl.currentPage -= 1
                      numberOfPagesLabel.text = &quot;\(pageControl.currentPage - 1)/\(pages.count)&quot;
                  }
      }
  </code></pre>
  <pre><code>
  class DetailsViewController: UIViewController, UICollectionViewDelegate,  UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate  {
  
       private var collectionView: UICollectionView!
      
      let pageControl = UIPageControl (frame: CGRect(x: 20, y: 120, width: 44, height: 44))
      let numberOfPagesLabel = UILabel (frame: CGRect(x: 20, y: 0, width: 44, height: 44))
  
      
      private var pages = [Pages]() {
        didSet {
          DispatchQueue.main.async {
            self.collectionView.reloadData()
          }
        }
      }
      
       override func viewDidLoad() {
           super.viewDidLoad()
        
          numberOfPagesLabel.layer.cornerRadius = 15.0
          numberOfPagesLabel.clipsToBounds = true
          numberOfPagesLabel.center.x = self.view.center.x
          numberOfPagesLabel.center.y = self.view.center.y+350
  
           
           self.view.addSubview(collectionView)
          self.view.addSubview(pageControl)
          self.view.addSubview(numberOfPagesLabel)
  
       }
  
           override func viewDidAppear(_ animated: Bool) {
             super.viewDidAppear(true)
              
              collectionViewLayout()
              collectionView.delegate = self
              collectionView.dataSource = self
  
              }
    
  
      
      func scrollViewDidScroll(_ scrollView: UIScrollView) {
  
          if pageControl.currentPage &lt; pages.count {
                     pageControl.currentPage += 1
                      numberOfPagesLabel.text = &quot;\(pageControl.currentPage + 1)/\(pages.count)&quot;
          } else if pageControl.currentPage != 0 {
                      pageControl.currentPage -= 1
                      numberOfPagesLabel.text = &quot;\(pageControl.currentPage - 1)/\(pages.count)&quot;
                  }
      }
      
       func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return pages.count
           }
  
  
       func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &quot;colCell7&quot;, for: indexPath) as! colCell7
          
           let book = pages[indexPath.row]
           cell.setupCell(for: book)
           return cell
       }
  
  
  
       func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
  
          if UIWindow.isLandscape {
              return CGSize(width: (collectionView.frame.size.width)/2, height: (collectionView.frame.size.height))
          }
  
          else {            return CGSize(width: (collectionView.frame.size.width), height: (collectionView.frame.size.height))
          }
      }
  
      
   func collectionViewLayout() {
          
          let layout = UICollectionViewFlowLayout()
          layout.scrollDirection = .horizontal
          collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), collectionViewLayout: layout)
         collectionView.backgroundColor = .white
  
         collectionView.showsHorizontalScrollIndicator = false
         collectionView.showsVerticalScrollIndicator = false
         collectionView.isPagingEnabled = true
  
  
          collectionView.translatesAutoresizingMaskIntoConstraints = false
          collectionView.register(colCell7.self, forCellWithReuseIdentifier: &quot;colCell7&quot;)
          collectionView.isScrollEnabled = true
         collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
         layout.itemSize = CGSize(width: view.frame.width, height: view.frame.height)
                layout.minimumLineSpacing = 0
                layout.minimumInteritemSpacing = 0
         layout.itemSize = CGSize(width: view.frame.width/2, height:view.frame.height )
      }
  }
  </code></pre>