【问题标题】:How does pagination work in collectionview?分页在collectionview中如何工作?
【发布时间】:2017-03-06 07:39:17
【问题描述】:

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

我的问题是如何在我的收藏视图中显示这个 nextPage 的图像?

{  
   "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)")

        }

   }

我的数据从 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
    }

【问题讨论】:

    标签: ios swift pagination uicollectionview


    【解决方案1】:

    请先check my this answer,在出现footerview 时向您的collectionview 添加一个footer view 进行网络调用,将新数组附加到先前的数组并重新加载您的collectionview

    【讨论】:

      【解决方案2】:

      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() 
      }
      

      【讨论】:

      • 我更喜欢使用默认值。我的意思是页脚视图/UICollectionElementKindSectionFooter
      • - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 您可以使用此方法检查最后一个单元格。
      猜你喜欢
      • 2016-10-10
      • 2011-04-02
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多