Reputation: 537
I came across a problem with a nested Collection View inside a Table View Cell. The content is uploaded from an online database and it takes a while to fetch the data. My question is how to keep reloading data for the Collection View until the content is fetched from the online database and then display it.
class DiscoverViewCell: UITableViewCell {
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var _collectionView: UICollectionView!
@IBAction func seeMoreAction(_ sender: Any) {
}
}
class MovieCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var moviePicture: UIImageView!
@IBOutlet weak var movieTitleLabel: UILabel!
func updateCollectionCell(movie: MovieModel){
moviePicture.image = movie.poster
movieTitleLabel.text = movie.name
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MovieCell", for: indexPath) as? MovieCollectionViewCell else {
return UICollectionViewCell()
}
cell.updateCollectionCell(movie: movieArray[indexPath.item])
return cell
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "DiscoverCell") as? DiscoverViewCell else {
return UITableViewCell()
}
cell.categoryLabel.text = categories[indexPath.item]
setUpCell(cell)
return cell
}
Also, how is it possible to display different Collection Views content inside Table View Cells depending of a label which is inside each Table View Cell but separated from Collection View.
Upvotes: 3
Views: 1918
Reputation: 2661
The best way to solve this problem is by containing all of the logic for determining if the data has already been retrieved, or is in the process of retrieving the data from the external source, in the model that is used to encapsulate all of the data being presented in the collection view.
So if you had a model for the tableViewCell, with a nested model for the nested collectionView, I personally would add an enumeration inside of this collectionViewModel to be used to determine the current state of that specific UICollectionView. So if the UITableView had many cells, for example, and the user was scrolling through them rapidly, each time the tableViewCell is dequeued, the state would be used to determine if the cell should show a spinner or if the data that was previously retrieved should be presented (Loading vs Loaded).
Upvotes: 0