如何在滚动到整页的地方实现 collectionview 的干净分页

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

我有一个集合视图,其中包含 3 宽 x 3 高的单元格网格。我已启用分页以水平滚动以查看下一页的 9 个单元格。但是,最后一页我没有完整的 9 个单元格,只剩下 3 个单元格。分页只滚动到第一列单元格,看起来很糟糕。我希望它完全滚动到第三页,以便它在第一列中显示剩余的 3 个单元格,在第二列中显示 6 个空单元格。任何帮助是极大的赞赏!这是我的分页代码,在此先感谢!

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)
}

当您得到将显示在 CollectionView 中的 Collection 时,您应该对它进行 9 取模以获得余数。如果模数为 0,则什么也不做。如果模数是任何其他数字,请从 9 中减去该数字,然后将那么多 "dummy" 个单元格添加到您的集合中以填充它们。

这里有一些代码可以帮助您入门:

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
    }
}