UICollectionView使用API 进行分页

问题描述 投票:-1回答:1

我有一个工作的应用程序,我使用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
    }
}

将根据要求提供更多代码。

ios swift alamofire
1个回答
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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.