分页视图中的分页如何工作?

时间:2017-03-06 07:39:17

标签: ios swift pagination uicollectionview

我正在尝试在演示应用中实现分页。我正在使用UICollectionView使用SDWebImage从API显示大量图像。 API支持这样的分页:

我的问题是如何将这个nextPage的图像显示到我的collectionview?

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

我的目标是将nextPageUrl的照片显示到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)")

        }

   }

我的数据从网址上获得了乐趣......

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
    }

2 个答案:

答案 0 :(得分:0)

首先请check my this answer,在显示footerview进行网络呼叫时,在您的集合视图中添加页脚视图,将新数组附加到上一个数组并重新加载您的集合视图

答案 1 :(得分: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() 
}