2017-03-03 22 views
0

我试图在演示应用程序中实现分页。我使用UICollectionView来使用SDWebImage(Swift)从API中显示大量图像。而API支持这样的分页:分页在iOS中如何工作? (Swift)

"pagination":{ 
    "total":355, 
    "totalPages":10, 
    "page":1, 
    "nextPage":2, 
    "nextPageUrl":"http://api..................?page=2" } 

那么,我该怎么做呢?任何建议或exsample代码?请帮忙!我是新雨燕在:)

回答

0

可以使用委托方法,为自动分页

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath){ 
    //Load your data and reload them, when the indexPath isEqual to the indexPath of last object 
} 

或者,你也可以内cellForItemAt

启用
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 
    //Load your data and reload them, when the indexPath isEqual to the indexPath of last object 
} 
+0

我想显示页码太 – SwiftyIso

+0

您从API得到页码,这样你就可以使用它们来展示,通过分配部分的页脚显示的页面没有。你收到里面的“分页” – Aashish

+0

你需要使用pageViewController,并点击页面编号,你可能会重新加载你的collectionView @SwiftyIso – Aashish

0

为下一个服务保留页面索引您的页面为pageNumber

var page = 0 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 
    print("page Num:\(page)") 
} 

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath){ 
    if arrImagesData.count-1 == indexPath.row && arrImagesData.count%10 == 0{ 
     getMoreImages(page) 
    } 
} 

func getMoreImages(page:Int){ 
    //hit api 
    if api_success == true { 
     if self.page == 0 { 
      self.arrImagesData.removeAll() 
     } 
    self.arrImagesData.appendContentsOf(api_data) 
    self.collectionImages.reloadData() 
    self.page = self.page + 1 
    } 
} 
+0

我知道,但我想这样做[链接](http://labs.daemon.com .au/uploads/default/49/2ffdd9d0dea1eef3.png) 在我的收藏视图下 – SwiftyIso

+0

您需要使用selectedIndex调用getMoreImages(selectIndex)。 –