ioscollectionviewscroll-paging

How to implement clean paging of collectionview where it scrolls to full page


I have a collectionview with a grid of cells that is 3 wide by 3 tall. I have paging enabled to scroll horizontally to see the next page of 9 cells. However, I don't have a full 9 cells for the last page, only 3 remaining cells. The paging only scrolls to the first column of cells and it looks bad. I want it to scroll fully to the third page so it displays the 3 remaining cells in the first column and and 6 empty ones in the second 2 columns. Any help is greatly appreciated! Here is the paging code I have, thanks in advance!

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

      // sets which page we are currently on
      let pageWidth:CGFloat = collectionView.frame.size.width
      let currentPage:CGFloat = collectionView.contentOffset.x / pageWidth

      // sets the page control to the current page
      pageControl.currentPage = Int(currentPage)
}

Solution

  • When you get the Collection that would be displayed in your CollectionView you should do a modulo on it by 9 to get the remainder. If the modulo is 0, do nothing. If the modulo is any other number, subtract that number from 9 and add that many "dummy" cells to your collection to fill them out.

    Here is some code to get you started:

    int remainder = (int)[self.collectionView numberOfItemsInSection:0] % 9;  //I am assuming you don't have more than one section
    if(remainder){
        for(i = 9; i > remainder; i--){
            //add a dummy item to collectionView
        }
    }