0

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)
}
tahoecoop
  • 378
  • 2
  • 9
  • 30

1 Answers1

2

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
    }
}
dstudeba
  • 8,878
  • 3
  • 32
  • 41
  • Thanks for the response! What you're saying makes sense, but I can't find anything online how to add the clear dummy cells. Is this a matter of making a second blank prototype cell and adding these to your example above? Or is there a different way? Thanks a ton for your help! – tahoecoop Oct 30 '15 at 19:34
  • You have a data source for the `collectionView` that is an array of 9x + 3 items correct? I don't know what the type of object in that array (`UIImage` perhaps?) but you can make 6 more of them and append them to the end. (if it was an image you could make it a blank transparent image). Adding a footer might be more elegant, but it is tougher. – dstudeba Oct 30 '15 at 21:56