我CollectionViews
中有两个ViewController
。
我想同时在屏幕上同时显示两者,但由于某种原因,第二个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。我不明白为什么它不起作用。
答案 0 :(得分:2)
似乎您正在返回正确数量的单元格以及用于不同集合视图的单元格。我怀疑一个问题是:
获取数据的部分可能在不同的线程上,因此一旦获得数据,也许尝试更新集合视图?
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()
}
}
如果这不起作用,请继续