Swift 两个集合视图,一个赢得';t显示细胞

Swift 两个集合视图,一个赢得';t显示细胞,swift,Swift,我在一个ViewController中有两个collectionview。 我想同时在屏幕上显示这两个单元格,但由于某些原因,第二个CollectionView不会显示任何单元格 这就是我所拥有的: override func viewDidLoad() { super.viewDidLoad() mainView.popularCollection.dataSource = self mainView.popularCollection.delegate =

我在一个
ViewController
中有两个
collectionview
。 我想同时在屏幕上显示这两个单元格,但由于某些原因,第二个CollectionView不会显示任何单元格

这就是我所拥有的:

    override func viewDidLoad() {
    super.viewDidLoad()

    mainView.popularCollection.dataSource = self
    mainView.popularCollection.delegate = self
    mainView.popularCollection.register(PopularMovieCell.self, forCellWithReuseIdentifier: popularCell)

    mainView.upcomingMoviesCollection.dataSource = self
    mainView.upcomingMoviesCollection.delegate = self
    mainView.upcomingMoviesCollection.register(UpcomingMovieCell.self, forCellWithReuseIdentifier: upcomingCell)

    //TODO: change popularMovies to trending movies.
    NetworkingManager.shared.getPopularMovies { (fetchedMovies) in
        self.fetchedPopularMovies = fetchedMovies
    }

    NetworkingManager.shared.getUpComingMovies { (upcomingMovies) in
        self.fetchedUpcomingMovies = upcomingMovies
    }
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {        
    if collectionView == mainView.upcomingMoviesCollection {
        return fetchedUpcomingMovies.count
    }
    return fetchedPopularMovies.count
}

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

    if collectionView == mainView.upcomingMoviesCollection {
        let upComingCell = collectionView.dequeueReusableCell(withReuseIdentifier: upcomingCell, for: indexPath) as! UpcomingMovieCell
        let movie = self.fetchedUpcomingMovies[indexPath.row]

        if let poster = movie.poster_path {
            upComingCell.movieImage.loadImageUsingCacheWithUrlString(urlString: baseImgUrl + poster)
        }else {
            upComingCell.movieImage.image = UIImage(named: "placeholder")
        }
        upComingCell.movieNameLabel.text = movie.title
        upComingCell.movieReleasedate.text = movie.release_date

        return upComingCell
    }


    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: popularCell, for: indexPath) as! PopularMovieCell
    let movie = self.fetchedPopularMovies[indexPath.row]
    var genreString = ""

    if let poster = movie.poster_path {
        cell.movieImage.loadImageUsingCacheWithUrlString(urlString: baseImgUrl + poster)
    }else {
        cell.movieImage.image = UIImage(named: "placeholder")
    }
    cell.movieNameLabel.text = movie.title
    cell.ratingScoreLabel.text = String(movie.vote_average)

    //A movie may have few genres, so we get every genre code of the specific movie, translating it to a genre name, and adding it to one string.
    for genre in movie.genre_ids {
        if let genre = genresById[genre] {
            genreString = genreString + " " + genre + ", "
            genreString = genreString.trimmingCharacters(in: .whitespacesAndNewlines)
        }
    }
    //Removing the last char in the string if it is ","
    if genreString.last == "," {
        genreString.removeLast()
    }
    cell.movieGenre.text = genreString

    return cell
}
在一些类似的问题中,我发现这段代码是正确的答案,但对我来说,由于某些原因,它不起作用


我查过了,我得到了fetchedUpcomingMoves.count数字0,但当我打印它时,我得到了20。我不明白它为什么不起作用。

您似乎返回了正确的单元格数量以及不同集合视图的单元格。我怀疑的一个问题是:

获取数据的部分可能位于不同的线程上,因此在获得数据后是否可以尝试更新集合视图

NetworkingManager.shared.getPopularMovies { (fetchedMovies) in
    self.fetchedPopularMovies = fetchedMovies
    DispatchQueue.main.async {
        mainView.popularCollection.reloadData()
    }
}

NetworkingManager.shared.getUpComingMovies { (upcomingMovies) in
    self.fetchedUpcomingMovies = upcomingMovies
    DispatchQueue.main.async {
        mainView.upcomingCollection.reloadData()
    }
}

如果不起作用,请继续操作

是否尝试将断点添加到numberOfItems部分,并查看项目计数是否正确?或者在cellForItemAt添加一个断点以查看是否正在提取丢失的单元格?@ShunzheM是的,我在返回中添加了0,但在之前打印时,我得到了20。
.reloadData()
设置值后(在网络完成块内)@GustavoVollbrecht谢谢。谢谢!现在工作得很好!美好的很高兴知道我的答案有帮助;)