使用API​​进行UICollectionView分页

时间:2018-11-18 11:57:12

标签: ios swift alamofire

我有一个正在运行的应用程序,正在使用AlamofireSwiftyJSON从flicr API中获取图像。现在,我想在返回的图像中包括分页。当应用程序启动时,我将返回5张图像,并且这个数目会增加。我想通过UICollectionView实现这一目标。

我的代码写在下面。任何帮助,将不胜感激。每页返回100个项目。

服务

func setData(json: JSON) -> Void {

        if let photos = json["photos"]["photo"].array {
            for item in photos {

                let photoID = item["id"].stringValue
                let farm = item["farm"].stringValue
                let server = item["server"].stringValue
                let secret = item["secret"].stringValue
                let title = item["title"].stringValue

                let imageString = "https://farm\(farm).staticflickr.com/\(server)/\(photoID)_\(secret)_b.jpg/"
                //could use _n.jpg too

                let flickrPhoto = FlickrPhotoModel(id: photoID, farm: farm, server: server, secret: secret, flickerImg: imageString, title: title)
                flickerPhotos.append(flickrPhoto)
            }
        }

    }

CONTROLLER

class MainVC: UIViewController {

     @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.delegate = self
        collectionView.dataSource = self


        ServiceProvider.instance.getPhotos { (success) in
            if success {
                self.collectionView.reloadData()
            }
        }
    }

}


extension MainVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

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

        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PHOTO_CELL, for: indexPath) as? MainCell {

             let flickerPhoto = ServiceProvider.instance.flickerPhotos[indexPath.row]

            cell.configueCell(flickerPhotoModel: flickerPhoto, index: indexPath.item)
            return cell
        }
        return MainCell()
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ServiceProvider.instance.flickerPhotos.count
    }
}

其他代码将根据要求提供。

1 个答案:

答案 0 :(得分:0)

请尝试使用此代码。

  func scrollViewDidScroll(_ scrollView: UIScrollView) {
   if isGetResponse {
    if (scrollView.contentOffset.y == scrollView.contentSize.height - scrollView.frame.size.height)
    {
        if totalArrayCount! > self.arrImage.count{

                isGetResponse = false
                activityIndiator?.startAnimating()
                self.view.bringSubview(toFront: activityIndiator!)
                pageIndex = pageIndex + 1
                print(pageIndex)
                self.getProductListing(withPageCount: pageIndex)
            }
        }

        //LOAD MORE
        // you can also add a isLoading bool value for better dealing :D
    }
}