Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift中为电影数据库分页_Ios_Swift_Pagination_Themoviedb Api - Fatal编程技术网

Ios 在swift中为电影数据库分页

Ios 在swift中为电影数据库分页,ios,swift,pagination,themoviedb-api,Ios,Swift,Pagination,Themoviedb Api,我用MovieDB API在swift中设置分页时遇到问题 通常你会有一个限制和一个offet,然后它会传递到你的模型数组。计数-1 使用CollectionView时 我正在使用一个可扩散的数据源,无法看到解决方案 有没有人能够实现这个或类似的功能 当前的API服务如下所示 class APIService { static let shared = APIService() //always pass in your first API so the one wh

我用MovieDB API在swift中设置分页时遇到问题 通常你会有一个限制和一个offet,然后它会传递到你的模型数组。计数-1 使用CollectionView时 我正在使用一个可扩散的数据源,无法看到解决方案 有没有人能够实现这个或类似的功能

当前的API服务如下所示

  class APIService {

      static let shared = APIService()

    //always pass in your first API so the one which holds title, release date ect
    func fetchMovies(completionHandler: @escaping ([Movie]?, Error?) -> ()) {

        guard let url = URL(string: APINOWPLAYING) else {
            print("not a valid url")
            return
        }
        let request = URLRequest(url: url)
        URLSession.shared.dataTask(with: request) { (data, response, error) in
            if let data = data {//when Decoding use the 2nd API model with the array 
                if let decodedResponse = try? JSONDecoder().decode(Movies.self, from: data) {

                    DispatchQueue.main.async {
                        completionHandler(decodedResponse.results, nil)
                        print("TOTAL RESULTS \(decodedResponse.page)")
                    }
                    return
                }
            }
            print("Fatch Failed \(error?.localizedDescription ?? "error unknown")")
        }.resume()

    }
视图控制器

  private func setupDiffableDataSource() {
        collectionView.dataSource = diffDataSource

        //MARK:- SetupHeader under Compositional Sections Extension
        setupHeader()

        APIService.shared.fetchMovies { (movies, err) in

            APIService.shared.fetchTopMovies { (movieGroup, err) in

                var snapshot = self.diffDataSource.snapshot()
                snapshot.appendSections([.topSection])
                snapshot.appendItems(movies ?? [], toSection: .topSection)

                snapshot.appendSections([.bottomSection])
                let objects = movieGroup?.results ?? []
                snapshot.appendItems(objects, toSection: .bottomSection)

                self.diffDataSource.apply(snapshot)
            }
        }
    }
有人知道如何使用API进行分页吗

这就是MOVIEDB api调用的样子

让我们开始玩吧= “”

希望有人能给我指出正确的方向


谢谢

您可以使用
func collectionView(\ucollectionview:UICollectionView,willDisplay cell:UICollectionViewCell,forItemAt indexPath:indexPath)
来自
UICollectionViewDelegate

您需要更新服务,以便它能够处理页面参数

var isCanLoadMore = false
var currentPage = 1

private func fetchData(page: Int) {
    // your API request
    // remember to change isCanLoadMore = true after apply(snapshot)
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    
    if isCanLoadMore {
        if diffableDataSource.snapshot().numberOfSections - 1 == indexPath.section {
            let currentSection = diffableDataSource.snapshot().sectionIdentifiers[indexPath.section]
            if diffableDataSource.snapshot().numberOfItems(inSection: currentSection) - 1 == indexPath.row {
                isCanLoadMore = false
                currentPage += 1
                print("NEXT PAGE")
                fetchData(page: currentPage)
            }
        }
    }
}