2017-03-06 40 views
1

我試圖在演示應用程序中實現分頁。我正在使用UICollectionView來使用SDWebImage從API顯示大量圖像。而API支持這樣的分頁:分頁在分頁視圖中如何工作?

我的問題是如何將這個nextPage的圖像顯示到我的collectionview?

{ 
    "meta":{ 
     "code":200 
    }, 
    "data":{ }, 
    "pagination":{ 
     "total":86, 
     "totalPages":3, 
     "page":1, 
     "nextPage":2, 
     "nextPageUrl":"http://.............?page=2" 
    } 
} 

而我的目標是,以顯示此nextPageUrl的PIC到的CollectionView。 這裏是我的代碼:

class StoreViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate { 

    @IBOutlet var MyStoreCollectionView: UICollectionView! 


    var alldata: [PopulerMagazalarData]? 
    var indexPath: IndexPath? 
    var storeData : [PopulerMagazalarData] = [] 

    let pagenumber = 1 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     if let indexPath = self.indexPath, let storeData = self.alldata?[indexPath.row] { 

     let storeusername = storeData.username 
     GetDataFromUrl(from: "https://............./\(storeusername!)?page=\(pagenumber)") 

     } 

    } 

而我得到的數據,從URL的樂趣...

func GetDataFromUrl(from:String){ 

     Alamofire.request(from, method: .get).validate().responseJSON { response in 
      switch response.result { 
      case .success(let value): 
       let json = JSON(value) 

       self.storeData = [PopulerMagazalarData]() 

             //...Creating Data Obj. 

         let data = PopulerMagazalarData() 

         let username = json["data"]["store"]["user"]["username"].string 
         let userpic = json["data"]["store"]["user"]["profilePicture"].string 
         let productsCount = json["data"]["store"]["productsCount"].int 
         let description = json["data"]["store"]["description"].string 
         let followedby = json["data"]["store"]["user"]["counts"]["followedBy"].int 

         let count:Int? = json["data"]["products"].array?.count 
         if let ct = count { 

          for index in 0...ct-1{ 

         let images = json["data"]["products"][index]["images"]["standart"]["url"].string 

           data.img1 = images 
          self.storeData.append(data) 
          } 
         } 

         //***************** 
         data.username = username 
         data.profilPic = userpic 
         data.producsCount = productsCount 
         data.desc = description 
         data.followedby = followedby 

         //****************** 
         self.storeData.append(data) 


        // for refresh collecitonView 
        self.refresh_now() 



      case .failure(let error): 
       print(error) 
      } 
     } 
    } 

    //...CollectionView ReloadData func... 
    func refresh_now(){ 

     DispatchQueue.main.async (
      execute: 
      { 
       self.MyStoreCollectionView.reloadData() 
     } 
     ) 

    } 

,這是我的CollectionView基金:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     return 1 
    } 

回答

0

首先,請在check my this answer的位置爲您的collectionview添加頁腳視圖footerview出現網絡電話,追加新陣列到先前的陣列,並重新加載您的collectionview

0

試試這個簡單的解決方案,幫助CCBottomRefreshControl你需要把它當作簡單的UIRefreshController

let bottomRefreshController = UIRefreshControl() 

bottomRefreshController.triggerVerticalOffset = 50 

bottomRefreshController.addTarget(self, action: #selector(ViewController.refreshBottom), forControlEvents: .ValueChanged) 

collectionView.bottomRefreshControl = bottomRefreshController 

func refreshBottom() { 
    //api call for loading more data 
    loadMoreData() 
} 
+0

我更喜歡使用默認值。我的意思是footerview/UICollectionElementKindSectionFooter –

+0

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 您可以使用此方法檢查最後一個單元格。 –