2017-03-03 21 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)。 –