Pagination with UICollectionView working with API

﹥>﹥吖頭↗ 提交于 2019-11-27 08:52:49

问题


I have a working application where I am using Alamofire and SwiftyJSON to fetch images from flicr API. Now, I want to include pagination in the returned images. I am returning 5 images when the application launches and this number can increase. I want to achieve this with the UICollectionView.

My codes are written below. Any help would be appreciated. 100 items are being returned on every page.

Services

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

Further codes would be supplied on request.


回答1:


Please try this code.

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


来源:https://stackoverflow.com/questions/53360597/pagination-with-uicollectionview-working-with-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!