2017-03-03 16 views
0

私はデモアプリケーションでページネーションを実装しようとしています。私はSDWebImage(Swift)を使用してAPIから多くの画像を表示するためにUICollectionViewを使用しています。 APIは次のようにページ分割をサポートしています:iOSでページ分割はどのように機能しますか? (スウィフト)

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

どうすればいいですか?アドバイスやサンプルコードはありますか?助けてください!私は、スウィフトに新しいです:)

答えて

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

は、私はあなたがいないページを表示するためにセクションのフッターを割り当てることにより、表示するためにそれらを使用することができますので、あなたは、あなたのAPIからページ番号を取得しているページ番号あまりに – SwiftyIso

+0

を示したいと思います。あなたはページ内を受け取る – Aashish

+0

pageViewControllerを使用する必要があります。ページ番号をクリックしてコレクションをリロードすることができます@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) myコレクションビュー – SwiftyIso

+0

selectedIndexでgetMoreImages(selectIndex)を呼び出す必要があります。 –

関連する問題