试图完成我的作业,我正在themoviedb.org上工作。 themoviedb.org中电影的当前值仅为20。滚动到底部时如何加载更多内容?我尝试了一些解决方案,但没有用。而且大多数解决方案都是针对TableView的。找不到对CollectionView有用的东西。

下面是我加载这20部电影并可以拉动更新的代码。

import UIKit

class List: UICollectionViewController {

    lazy var refresher: UIRefreshControl = {
        ...
    }()

    @IBOutlet var movieCollectionView: UICollectionView!
    @IBOutlet weak var flowLayout: UICollectionViewFlowLayout!

    var movies: [Movie] = [Movie]()

    override func viewDidLoad() {
        super.viewDidLoad()

        movieCollectionView.refreshControl = refresher
        ...
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        fetchMovies()
    }


// Update Movies List
    @objc func fetchMovies() {
        ViewController.sharedInstance().fetchMovies { (movies) in
            if let movies = movies {
                self.movies = movies
                DispatchQueue.main.async {
                    self.movieCollectionView.reloadData()
                }
            } else {
                print("error occured while fetching movies")
            }
        }

        let deadline = DispatchTime.now() + .milliseconds(700)
        DispatchQueue.main.asyncAfter(deadline: deadline) {
            self.refresher.endRefreshing()
        }
    }


// CollectionView Functions
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return movies.count
    }

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

        let cellReuseIdentifier = "collectionCell"
        let movie = movies[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseIdentifier, for: indexPath) as! CustomCollectionViewCell

        cell.poster!.contentMode = UIView.ContentMode.scaleAspectFit

        if let posterPath = movie.posterPath {
            ViewController.sharedInstance().fetchPoster(path: posterPath, size: ViewController.PosterConstants.RowPoster, completion: { (imageData) in
                if let image = imageData {
                    DispatchQueue.main.async {
                        cell.poster!.image = image
                    }
                } else {
                    print("error")
                }
            })
        }
        return cell
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let controller: Detail = storyboard?.instantiateViewController(withIdentifier: "Detail") as! Detail
        controller.movie = movies[indexPath.row]
        navigationController?.pushViewController(controller, animated: true)
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        var numberOfSections = 0
        if movies.count > 0 {
            numberOfSections = 1
            collectionView.backgroundView = nil

        } else {
            ...
            noDataLabel.text = "Please wait... I'm thinking..."
            ...
        }
        return numberOfSections
    }


}

最佳答案

您可以检查此存储库
https://github.com/mfmansueli/TMDB-Involves
我去年使用tmdb创建了这个项目,但我不知道这项工作还没有完成。

我在此类中使用无限滚动:
https://github.com/mfmansueli/TMDB-Involves/blob/master/IMBDInvolves/Scenes/Movies/MoviesViewController.swift

extension MoviesViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let height = scrollView.frame.height
    let contentSizeHeight = scrollView.contentSize.height
    let offset = scrollView.contentOffset.y
    let reachedBottom = (offset + height == contentSizeHeight)

    if reachedBottom {
        scrollViewDidReachBottom(scrollView)
    }
}

func scrollViewDidReachBottom(_ scrollView: UIScrollView) {
    refreshControl.beginRefreshing()
    viewModel.input.getUpcomingMovies()
}
}

关于ios - 在CollectionView中滚动到底部时加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55848905/

10-11 22:22
查看更多