【问题标题】:Pagination with UICollectionView working with API使用 API 的 UICollectionView 分页
【发布时间】:2018-11-18 11:57:11
【问题描述】:

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

    }

控制器

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】:

请试试这个代码。

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

【讨论】:

  • 谢谢。对我来说最好使用scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height
猜你喜欢
  • 2014-08-09
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多