Set an array of arrays inside of UICollectionView that's inside a UITableView and return the correct number of items?

I am trying to get a UITableView with a UICollectionView to show the following data set correctly inside the UICollectionViews.
the array which contains all this information is
Code Block
arregloDeArreglos

here's the data
Code Block
▿ 2 elements
▿ 0 : 1 element
▿ 0 : arregloBidimencional
▿ arregloDeEpisodios : Optional<Array<episodess>>
▿ some : 12 elements
▿ 0 : episodess
▿ episode_number : Optional<Int>
- some : 1
▿ name : Optional<String>
- some : "121045"
▿ still_path : Optional<String>
- some : "b9hFk5kwRcFUorxLQsOHasJnbDH"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 1 : episodess
▿ episode_number : Optional<Int>
- some : 2
▿ name : Optional<String>
- some : "131045"
▿ still_path : Optional<String>
- some : "/mSZDdQLHnDousFg35ndlmv42908"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 2 : episodess
▿ episode_number : Optional<Int>
- some : 3
▿ name : Optional<String>
- some : "181045"
▿ still_path : Optional<String>
- some : "/57bidANnv5zdPmJckmPL9f4Ogb7"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 3 : episodess
▿ episode_number : Optional<Int>
- some : 4
▿ name : Optional<String>
- some : "291045"
▿ still_path : Optional<String>
- some : "/swmIOzEJ8uIjCdd5B4w9Ln5qXsJ"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 4 : episodess
▿ episode_number : Optional<Int>
- some : 5
▿ name : Optional<String>
- some : "301045"
▿ still_path : Optional<String>
- some : "/2x9v9YupwZnMBnvCUOwaOv8UEQV.jpg"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 5 : episodess
▿ episode_number : Optional<Int>
- some : 6
▿ name : Optional<String>
- some : "311045"
▿ still_path : Optional<String>
- some : "/uFY25TSNrOxbL18jDV1pSjWPKEK"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 6 : episodess
▿ episode_number : Optional<Int>
- some : 7
▿ name : Optional<String>
- some : "011145"
▿ still_path : Optional<String>
- some : "/8LGArTKq5oMnKR9AjdRd3ocxJWu"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 7 : episodess
▿ episode_number : Optional<Int>
- some : 8
▿ name : Optional<String>
- some : "021145"
▿ still_path : Optional<String>
- some : "/yEBAannVEt2QhyUKyjlXhXrEVld"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 8 : episodess
▿ episode_number : Optional<Int>
- some : 9
▿ name : Optional<String>
- some : "031145"
▿ still_path : Optional<String>
- some : "/nf1zvR7D4kN1Xa1wlxXeuUrWpOo"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 9 : episodess
▿ episode_number : Optional<Int>
- some : 10
▿ name : Optional<String>
- some : "130146"
▿ still_path : Optional<String>
- some : "/1lTlSWY4chqpv1ibeQz4eYHaePf"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 10 : episodess
▿ episode_number : Optional<Int>
- some : 11
▿ name : Optional<String>
- some : "140146"
▿ still_path : Optional<String>
- some : "/qTKxjZTbiCqZ6lywwfmfTLX8QnK"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 11 : episodess
▿ episode_number : Optional<Int>
- some : 12
▿ name : Optional<String>
- some : "150146"
▿ still_path : Optional<String>
- some : "/8duv3ltJa7Olb721zsqcbss0kVx"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ seasonNumber : Optional<Int>
- some : 0
▿ 1 : 1 element
▿ 0 : arregloBidimencional
▿ arregloDeEpisodios : Optional<Array<episodess>>
▿ some : 11 elements
▿ 0 : episodess
▿ episode_number : Optional<Int>
- some : 1
▿ name : Optional<String>
- some : "Episode 1"
▿ still_path : Optional<String>
- some : "/24VlK7Ed5VFa0yE2kEmDPzyYks"
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ 1 : episodess
▿ episode_number : Optional<Int>
- some : 2
▿ name : Optional<String>
- some : "Episode 2"
▿ still_path : Optional<String>
- some : "/eoyTsCYEAymTAhBgXIbIfW2jrwz"
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ 2 : episodess
▿ episode_number : Optional<Int>
- some : 3
▿ name : Optional<String>
- some : "Episode 3"
▿ still_path : Optional<String>
- some : "/1EgiONNX9CgO6i4DjyeVBvsvT9v"
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ 3 : episodess
▿ episode_number : Optional<Int>
- some : 4
▿ name : Optional<String>
- some : "Episode 4"
▿ still_path : Optional<String>
- some : "/4lZnqwPkNmnVEZ6ipID92BiWDkd"
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ 4 : episodess
▿ episode_number : Optional<Int>
- some : 5
▿ name : Optional<String>
- some : "Episode 5"
▿ still_path : Optional<String>
- some : "/4wkH3s9d7YFvnjvQ24rS1pj4oUk"
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ 5 : episodess
▿ episode_number : Optional<Int>
- some : 6
▿ name : Optional<String>
- some : "Episode 6"
▿ still_path : Optional<String>
- some : "/46XRAOhySNHl1O0qTHMUdUxfDcA"
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ 6 : episodess
▿ episode_number : Optional<Int>
- some : 7
▿ name : Optional<String>
- some : "Episode 7"
▿ still_path : Optional<String>
- some : "/lbVYNXOtQSVU69ee7qbHWJ1f7b8"
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ 7 : episodess
▿ episode_number : Optional<Int>
- some : 8
▿ name : Optional<String>
- some : "Episode 8"
▿ still_path : Optional<String>
- some : "/95UudD6foD0V7xJc345sJSZ9vUW"
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ 8 : episodess
▿ episode_number : Optional<Int>
- some : 9
▿ name : Optional<String>
- some : "Episode 9"
▿ still_path : Optional<String>
- some : ""
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ 9 : episodess
▿ episode_number : Optional<Int>
- some : 10
▿ name : Optional<String>
- some : "Episode 10"
▿ still_path : Optional<String>
- some : ""
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ 10 : episodess
▿ episode_number : Optional<Int>
- some : 11
▿ name : Optional<String>
- some : "Episode 11"
▿ still_path : Optional<String>
- some : ""
▿ seasonNumerEpisode : Optional<Int>
- some : 2
▿ seasonNumber : Optional<Int>
- some : 1



[![dads][1]][1]


However, when I scroll horizontally inside the UICollectionViews, the data will get all messed up.


[1]: https:// i.stack. imgur. com/z9bw7 . png
[2]: https://i. stack. imgur.com/lCS13 . png

as you can see in both pictures

here's my code

table delegate and data source
Code Block
extension SelectedShowVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOfSeasonsInt.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "seasonsRegister", for: indexPath) as? NumberOfSeasonsCell else {
return UITableViewCell()
}
cell.selectionStyle = .none
cell.backgroundColor = .clear
//note for each cell i manually asign an different array
if indexPath.row == 0 {
let arreglo = arraySeasonsEpisodes //array which contains all the information from the first season
modelOfSeason = arreglo // to a global variable i asign the content the of adove season
let arregloDeArregloss = arregloBidimencional(arreglo: arreglo, tag: 0) // this is for saving the adove array (the season) to another array
arregloDeArreglos.append([arregloDeArregloss])
}
else if indexPath.row == 1{
let arreglo = arraySeasonsEpisodes2 //array which contains all the information from the second season
modelOfSeason = arreglo // to a global variable i asign the content the of adove season
let arregloDeArregloss = arregloBidimencional(arreglo: arreglo, tag: 1)// this is for saving the adove array (the season) to another array
arregloDeArreglos.append([arregloDeArregloss])
}
else if indexPath.row == 2{
let arreglo = arraySeasonsEpisodes3
modelOfSeason = arreglo
let arregloDeArregloss = arregloBidimencional(arreglo: arreglo, tag: 2)
arregloDeArreglos.append([arregloDeArregloss])
}
else if indexPath.row == 3{
let arreglo = arraySeasonsEpisodes4
modelOfSeason = arreglo
let arregloDeArregloss = arregloBidimencional(arreglo: arreglo, tag: 3)
arregloDeArreglos.append([arregloDeArregloss])
}
else if indexPath.row == 4{
let arreglo = arraySeasonsEpisodes5
modelOfSeason = arreglo
let arregloDeArregloss = arregloBidimencional(arreglo: arreglo, tag: 4)
arregloDeArreglos.append([arregloDeArregloss])
}
else if indexPath.row == 5{
let arreglo = arraySeasonsEpisodes6
modelOfSeason = arreglo
let arregloDeArregloss = arregloBidimencional(arreglo: arreglo, tag: 5)
arregloDeArreglos.append([arregloDeArregloss])
}
else if indexPath.row == 6{
let arreglo = arraySeasonsEpisodes7
modelOfSeason = arreglo
let arregloDeArregloss = arregloBidimencional(arreglo: arreglo, tag: 6)
arregloDeArreglos.append([arregloDeArregloss])
}
else if indexPath.row == 7{
let arreglo = arraySeasonsEpisodes8
modelOfSeason = arreglo
let arregloDeArregloss = arregloBidimencional(arreglo: arreglo, tag: 7)
arregloDeArreglos.append([arregloDeArregloss])
}
else if indexPath.row == 8{
let arreglo = arraySeasonsEpisodes9
modelOfSeason = arreglo
let arregloDeArregloss = arregloBidimencional(arreglo: arreglo, tag: 8)
arregloDeArreglos.append([arregloDeArregloss])
}
else if indexPath.row == 9{
let arreglo = arraySeasonsEpisodes10
modelOfSeason = arreglo
let arregloDeArregloss = arregloBidimencional(arreglo: arreglo, tag: 9)
arregloDeArreglos.append([arregloDeArregloss])
}
else{
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let tableViewCell = cell as? NumberOfSeasonsCell else { return }
//this method is for saving the offset and delegate for each cell
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//this method is for saving the offset and delegate for each cell
guard let tableViewCell = cell as? NumberOfSeasonsCell else { return }
storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
}
}

uicollection delegate and datasource
Code Block
extension SelectedShowVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arraySeasonsEpisodes.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifierSeasons", for: indexPath) as! SeasonCell
cell.backgroundColor = .clear
//global variable assign to the model for uicollectionview individual cell
let arreglo = modelOfSeason[indexPath.item]
cell.model = arreglo
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 180, height: 200)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 30
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
}
}


uiCollectionviewCell
Code Block
class SeasonCell: UICollectionViewCell {
var model: episodess? {
didSet {
guard let viewModel = model else { return }
let episodio = viewModel.episode_number
let nombre = viewModel.name
let guardado = viewModel.still_path
episodeNumber.text = "Episode Number: \(episodio ?? 0)"
episodeName.text = "Episode Name: \(nombre ?? "")"
let image = "w500\(guardado ?? "")"
if image == "" || image == "undefined" {
seasonImage.image = UIImage()
seasonImage.contentMode = .scaleAspectFill
}else{
seasonImage.downloaded(from: image)
seasonImage.contentMode = .scaleAspectFill
}
}
}
override func prepareForReuse() {
super.prepareForReuse()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
let seasonImage: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleToFill
iv.backgroundColor = .clear
iv.image = UIImage(named: "")
iv.translatesAutoresizingMaskIntoConstraints = false
return iv
}()
var episodeNumber: UILabel = {
let label = UILabel()
label.text = ""
label.numberOfLines = 1
label.textAlignment = .left
label.font = Fonts.AVENIR.of(size: 10)
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.white
return label
}()
var episodeName: UILabel = {
let label = UILabel()
label.text = ""
label.numberOfLines = 1
label.textAlignment = .left
label.font = Fonts.AVENIR.of(size: 10)
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.white
return label
}()
func setupViews() {
addSubview(seasonImage)
addSubview(episodeNumber)
addSubview(episodeName)
NSLayoutConstraint.activate([
seasonImage.centerYAnchor.constraint(equalTo: self.centerYAnchor),
seasonImage.centerXAnchor.constraint(equalTo: self.centerXAnchor),
seasonImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1),
seasonImage.heightAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.6),
episodeNumber.leftAnchor.constraint(equalTo: seasonImage.leftAnchor),
episodeNumber.topAnchor.constraint(equalTo: seasonImage.bottomAnchor, constant: 8),
episodeNumber.rightAnchor.constraint(equalTo: seasonImage.rightAnchor),
episodeName.leftAnchor.constraint(equalTo: episodeNumber.leftAnchor),
episodeName.rightAnchor.constraint(equalTo: seasonImage.rightAnchor),
episodeName.topAnchor.constraint(equalTo: episodeNumber.bottomAnchor, constant: 8),
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

Answered by Claude31 in 665567022
What you need to do is populate both the tableViewCell and the collection inside.
The mechanism to define a dataSource is the following:
  • you declare the dataSource as self when you create the Collection

  • then, it all occurs in 

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

This dataSource function is defined in the class (which corresponds to the self above).

So, when you create the tableView cell, you have to declare dataSource for the CollectionView
Code Block
   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
seasonsCollectionView.dataSource = self

Now, in 
Code Block
collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)

 you need to use the correct part of the array that contains all collections.
Something like
Code Block
 cell.seasonsCollectionView.model = // Something


the model should be of type episodess
So, probably coming from a 2 dimension array of episodess, that correspond to:
  • the tableView cell row (contained in its tag ?) where the collection is : that should give an array of episodess

  • in this array, the episodess for the cellForItemAt indexPath.row

Hope that helps.
Can you show the whole code of SelectedShowVC?
As I suggested in your previous thread, we need to know how UITableViews and UICollectionViews are managed, including their dataSources.
Especially, we need to know how arregloDeArreglos, modelOfSeason or any other data used for dataSource.

here's the data

Can you show the data as a JSON text? Which makes very, very easier to test your code.

And please show the definition of your table view cell which is currently used.
Here's the data as JSON



here's the whole VC





Especially, we need to know how arregloDeArreglos, modelOfSeason or any other data used for dataSource.


Code Block
var modelOfSeason = arraySeasonsEpisodes

its only used as a global variable

and arregloDeArreglos
it's only used in cellForRowAt





its only used as a global variable 

Why are you making it global!!! Incredible...

These lines all causes Cannot find in scope error:
Code Block
arrayCast.removeAll()
arrayCreator.removeAll()
arrayAiringSeason.removeAll()
arrayOfSeasonsInt.removeAll()
arrayCreatorName.removeAll()
arraySeasonsEpisodes.removeAll()
arregloDeArreglos.removeAll()
arraySeasonsEpisodes.removeAll()
arraySeasonsEpisodes2.removeAll()
arraySeasonsEpisodes3.removeAll()
arraySeasonsEpisodes4.removeAll()
arraySeasonsEpisodes5.removeAll()
arraySeasonsEpisodes6.removeAll()
arraySeasonsEpisodes7.removeAll()
arraySeasonsEpisodes8.removeAll()
arraySeasonsEpisodes9.removeAll()
arraySeasonsEpisodes10.removeAll()

All of them are globals?
😅 sorry i have not experienced with this.
i don't have the knowledge to manage information from a UItable to UIcolectionview
so the only way i could solve it is making a global variable to move the previous information and loaded to the UIcollectionview

now i'm trying to investigate how can i send information from the table to collection
i'm trying to make it the same way i treated the information in the UICollectionViewCell
using didSet but still not clue

here's the code so far

Code Block
import UIKit
var modelOfSeason = arraySeasonsEpisodes //this is the global
class NumberOfSeasonsCell: UITableViewCell, ViewDelegate {
    var modelo: arregloBidimencional? {
        didSet {
            guard let viewModel = modelo else { return }
            let array = viewModel.arregloDeEpisodios
            
            
        }
    }
    
    var compactConstraints: [NSLayoutConstraint] = [NSLayoutConstraint]()
    var regularConstraints: [NSLayoutConstraint] = [NSLayoutConstraint]()
    var largeConstraints: [NSLayoutConstraint] = [NSLayoutConstraint]()
    
    
    lazy var seasonsCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 30
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.clear
        cv.register(SeasonCell.self, forCellWithReuseIdentifier: "identifierSeasons")
        cv.showsHorizontalScrollIndicator = false
        cv.translatesAutoresizingMaskIntoConstraints = false
        return cv
    }()
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    func contenido(array: arregloBidimencional){
        
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initComponents()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) {
        seasonsCollectionView.delegate = dataSourceDelegate
        seasonsCollectionView.dataSource = dataSourceDelegate
        seasonsCollectionView.tag = row
        seasonsCollectionView.setContentOffset(seasonsCollectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
        seasonsCollectionView.reloadData()
    }
    
    
    var collectionViewOffset: CGFloat {
        set { seasonsCollectionView.contentOffset.x = newValue }
        get { return seasonsCollectionView.contentOffset.x }
    }
    
    
    func initComponents() {
        addComponents()
        setAutolayout()
        activateCurrentLayout()
        
    }
    
    func addComponents() {
        self.addSubview(seasonsCollectionView)
        
    }
    
    func setAutolayout() {
        largeConstraints = [
            seasonsCollectionView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
            seasonsCollectionView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            seasonsCollectionView.widthAnchor.constraint(equalTo: self.widthAnchor),
            seasonsCollectionView.heightAnchor.constraint(equalTo: self.heightAnchor),
        ]
    }
    
    func activateCurrentLayout() {
        NSLayoutConstraint.activate(largeConstraints)
    }
}
extension UITableViewCell {
    open override func addSubview(_ view: UIView) {
        super.addSubview(view)
        sendSubviewToBack(contentView)
    }
}

yes all of then are global 😅

Code Block
var imagenArraaay: [String] = []
var arrayPoster_path: [String] = []
var arrayOverview: [String] = []
var arrayCreatorName: [String] = []
var season_number: String = ""
var season_number2: Int = 0
var air_date: String = ""
var still_path: String = ""
var arrayName: [String] = []
var arrayFirst_air_date: [String] = []
var arrayBackdrop_path: [String] = []
var arrayVote_average: [NSNumber] = []
var arrayid: [NSNumber] = []
var arrayShows: [tvshows] = []
var arrayCast: [castt] = []
var arrayCreator: [created] = []
var arrayAiringSeason: [episode] = []
var arraySeasonsEpisodes: [episodess] = []
var arraySeasonsEpisodes2: [episodess] = []
var arraySeasonsEpisodes3: [episodess] = []
var arraySeasonsEpisodes4: [episodess] = []
var arraySeasonsEpisodes5: [episodess] = []
var arraySeasonsEpisodes6: [episodess] = []
var arraySeasonsEpisodes7: [episodess] = []
var arraySeasonsEpisodes8: [episodess] = []
var arraySeasonsEpisodes9: [episodess] = []
var arraySeasonsEpisodes10: [episodess] = []
var arregloDeArreglos: [[arregloBidimencional]] = []
var arregloDeArreglosTri: [arregloTridimencional] = []
var arrayOfSeasonsInt: [Int] = []


here's the structs

Code Block
struct tvshows: Initiable {
    
    let poster_path: String?
    let name: String?
    let first_air_date: String?
    let vote_average: Double?
    let overview: String?
    let id: Int?
    let backdrop_path: String?
    init() {
        
        self.poster_path = ""
        self.name = ""
        self.first_air_date = ""
        self.vote_average = 0.0
        self.overview = ""
        self.id = 0
        self.backdrop_path = ""
        }
    
    init(dictionary: NSDictionary) {
        self.poster_path = dictionary["poster_path"] as? String ?? ""
        self.name = dictionary["name"] as? String ?? ""
        self.first_air_date = dictionary["first_air_date"] as? String ?? ""
        self.vote_average = dictionary["vote_average"] as? Double ?? 0.0
        self.overview = dictionary["overview"] as? String ?? ""
        self.backdrop_path = dictionary["backdrop_path"] as? String ?? ""
        self.id = dictionary["id"] as? Int ?? 0
    }
    
}
struct imageStruct: Initiable {
    private var image: [String] = [""]
    
    init() {
        
    }
      
    init(image: [String]) {
        self.image = image
    }
}


Code Block
struct castt: Initiable {
    
    let original_name: String?
    let character: String?
    let profile_path: String?
    
    init() {
        
        self.original_name = ""
        self.character = ""
        self.profile_path = ""
        }
    
    init(dictionary: NSDictionary) {
        self.original_name = dictionary["original_name"] as? String ?? ""
        self.character = dictionary["character"] as? String ?? ""
        self.profile_path = dictionary["profile_path"] as? String ?? ""
    }
    
}
struct created: Initiable {
    
    let name: String?
    
    
    init() {
        
        self.name = ""
        
        }
    
    init(dictionary: NSDictionary) {
        self.name = dictionary["name"] as? String ?? ""
        
    }
    
}


Code Block
struct episodess: Initiable {
    let episode_number: Int?
    let name: String?
    let still_path: String?
    let seasonNumerEpisode: Int?
    init() {
        
            self.episode_number = 0
            self.name = ""
            self.still_path = ""
            self.seasonNumerEpisode = 0
        }
    
    init(dictionary: NSDictionary, seasonInt: Int) {
        self.episode_number = dictionary["episode_number"] as? Int ?? 0
        self.name = dictionary["name"] as? String ?? ""
        self.still_path = dictionary["still_path"] as? String ?? ""
        self.seasonNumerEpisode = seasonInt
    }
    
}
struct arregloBidimencional: Initiable {
    let arregloDeEpisodios: [episodess]?
    let seasonNumber: Int?
    
    init() {
        
        self.arregloDeEpisodios = [episodess]()
        self.seasonNumber = 0
        
        }
    
    init(arreglo: [episodess], tag: Int) {
        self.arregloDeEpisodios = arreglo
        self.seasonNumber = tag
    }
}
struct arregloTridimencional: Initiable {
    let arregloDeArreglos: [arregloBidimencional]?
    let seasonNumberArreglo: Int?
    
    init() {
        
        self.arregloDeArreglos = [arregloBidimencional]()
        self.seasonNumberArreglo = 0
        
        }
    
    init(arregloTri: [arregloBidimencional], tag: Int) {
        self.arregloDeArreglos = arregloTri
        self.seasonNumberArreglo = tag
    }
}
one is missing but i don't used it

yes all of then are global

Well, frankly saying, you may need a lot more emojis which looks more severe.

I often say to developers who say themselves as beginners,

if you need effort to keep two or more arrays consistent, it is an indication of bad design and you should re-organize your code.


You should better avoid using globals just for data passing.

And you are still using non-Capitalized name for structs, that is making your code harder to read.


Anyway, you have multiple UICollectionViews in each UITableView, so each UICollectionView needs to have own its dataSource.
Only one global modelOfSeason would never work appropriately for multiple UICollectionViews.

I will try to find how to fix your code with minimum modification, but it may take long.


By the way, this thread is being too heavy for the Safari of my old MacBook Air.
Maybe too many Code block (<>) is not good for Safari, please try Text attachment next time.
How are all arraySeasonsEpisodesX declared ?
if it is arraySeasonsEpisodesX : SomeType

Why not an array
arraySeasonsEpisodes : [SomeType]

Then no need for the individual tests.
I moved to FireFox. Seems FireFox works well with this site and I can write a reply.

I struggled with your code for hours, and I think the following code would be able to make your collection views work:
(I renamed all the struct names to follow the coding convection of Swift, if you have some specific reason you cannot follow it, please re-interpret them appropriately.)

globals

Code Block
var imagenArraaay: [String] = []
var arrayPoster_path: [String] = []
var arrayOverview: [String] = []
var arrayCreatorName: [String] = []
var season_number: String = ""
var season_number2: Int = 0
var air_date: String = ""
var still_path: String = ""
var arrayName: [String] = []
var arrayFirst_air_date: [String] = []
var arrayBackdrop_path: [String] = []
var arrayVote_average: [NSNumber] = []
var arrayid: [NSNumber] = []
var arrayShows: [Tvshows] = []
var arrayCast: [Castt] = []
var arrayCreator: [Created] = []
var arrayAiringSeason: [Episode] = []
//#↓ Remove these many Arrays
//var arraySeasonsEpisodes: [Episodess] = []
//var arraySeasonsEpisodes2: [Episodess] = []
//var arraySeasonsEpisodes3: [Episodess] = []
//var arraySeasonsEpisodes4: [Episodess] = []
//var arraySeasonsEpisodes5: [Episodess] = []
//var arraySeasonsEpisodes6: [Episodess] = []
//var arraySeasonsEpisodes7: [Episodess] = []
//var arraySeasonsEpisodes8: [Episodess] = []
//var arraySeasonsEpisodes9: [Episodess] = []
//var arraySeasonsEpisodes10: [Episodess] = []
//#↓ And use one Array of Array instead
var arrayOfArraySeasonsEpisodes: [[Episodess]] = .init(repeating: [], count: 10)
var arregloDeArreglos: [[ArregloBidimencional]] = []
var arregloDeArreglosTri: [ArregloTridimencional] = []
var arrayOfSeasonsInt: [Int] = []


SelectedShowVC

Code Block
override func viewWillAppear(_ animated: Bool) {
arrayCast.removeAll()
arrayCreator.removeAll()
arrayAiringSeason.removeAll()
arrayOfSeasonsInt.removeAll()
arrayCreatorName.removeAll()
// arraySeasonsEpisodes.removeAll() //# duplicate
arregloDeArreglos.removeAll()
//#↓ Remove these
// arraySeasonsEpisodes.removeAll()
// arraySeasonsEpisodes2.removeAll()
// arraySeasonsEpisodes3.removeAll()
// arraySeasonsEpisodes4.removeAll()
// arraySeasonsEpisodes5.removeAll()
// arraySeasonsEpisodes6.removeAll()
// arraySeasonsEpisodes7.removeAll()
// arraySeasonsEpisodes8.removeAll()
// arraySeasonsEpisodes9.removeAll()
// arraySeasonsEpisodes10.removeAll()
arrayOfArraySeasonsEpisodes = .init(repeating: [], count: 10) //#<- Use Array (of Array)
//...
UI.tvTitleLabel.text = UI.SelectedNamee
UI.descriptionTV.text = UI.SelectedOverVieww
UI.scoreLabel.text = UI.SelectedVotee
}

(As modest as possible, your code is sort of a showcase of bad practices. Especially, getCastInfo and getCreators (with included getSeasons) are critically bad and you should better re-write them as soon as possible if your code is not just an exercise. But this is another issue.)

NumberOfSeasonsCell

Code Block
//#↓ Extension to use `tag` of collection view as `season`
extension NumberOfSeasonsCell {
func setCollectionViewTag(_ tag: Int) {
self.seasonsCollectionView.tag = tag
}
}



I think the following code would be able to make your collection views work

i'm sorry how can update with the following code my cellForRowAt for the UITable and the cellForItemAt for the UICollection
i didn't quite understand
how can i use this part
Code Block
arrayOfArraySeasonsEpisodes = .init(repeating: [], count: 10) //#<- Use Array (of Array)

how can i use this part

Please put the exact line into the place I have show, inside viewWillAppear of SelectedShowVC.

i'm sorry how can update with the following code my cellForRowAt for the UITable and the cellForItemAt for the UICollection
i didn't quite understand

And, sorry, I have missed to post some parts (core parts) of code needed:

SelectedShowVC (extension)

Code Block
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView === UI.castCollectionView { //#<- Use `===` instead of `==`
return arrayCast.count
} else {
//#↓ use `tag` as `season`
let season = collectionView.tag
guard (1...10) ~= season else {return 0}
return arrayOfArraySeasonsEpisodes[season-1].count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView === UI.castCollectionView { //#<- Use `===`
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifierCast", for: indexPath) as! CastCell
cell.backgroundColor = .clear
cell.model = arrayCast[indexPath.item]
return cell
} else {
//#↓ Use `tag` as `season`
let season = collectionView.tag
guard (1...10) ~= season else {return UICollectionViewCell()}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifierSeasons", for: indexPath) as! SeasonCell
cell.backgroundColor = .clear
//global variable assign to the model for uicollectionview individual cell
let arreglo = arrayOfArraySeasonsEpisodes[season-1][indexPath.item] //#<-
cell.model = arreglo
return cell
}
}


Code Block
func getSeasons(url: String) {
//...
for category in toneCategories {
//#↓ You can simplify many `if`s using Array
if (1...10) ~= season {
let show = Episodess(dictionary: category, seasonInt: season)
arrayOfArraySeasonsEpisodes[season-1].append(show)
}
}
//...
}


Code Block
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "seasonsRegister", for: indexPath) as? NumberOfSeasonsCell else {
return UITableViewCell()
}
cell.selectionStyle = .none
cell.backgroundColor = .clear
//note for each cell i manually asign an different array -> #Better not do it
let season = indexPath.row + 1
guard (1...10) ~= season else {return UITableViewCell()}
cell.setCollectionViewTag(season) //#<- Use `tag` as `season`
//#↓ I do not understand why you need this
//# Generally, you should never modify data in `tableView(_:cellForRowAt:)`
let arreglo = arrayOfArraySeasonsEpisodes[season-1]
let arregloDeArregloss = ArregloBidimencional(arreglo: arreglo, tag: 0)
arregloDeArreglos.append([arregloDeArregloss])
return cell
}





Accepted Answer
What you need to do is populate both the tableViewCell and the collection inside.
The mechanism to define a dataSource is the following:
  • you declare the dataSource as self when you create the Collection

  • then, it all occurs in 

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

This dataSource function is defined in the class (which corresponds to the self above).

So, when you create the tableView cell, you have to declare dataSource for the CollectionView
Code Block
   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
seasonsCollectionView.dataSource = self

Now, in 
Code Block
collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)

 you need to use the correct part of the array that contains all collections.
Something like
Code Block
 cell.seasonsCollectionView.model = // Something


the model should be of type episodess
So, probably coming from a 2 dimension array of episodess, that correspond to:
  • the tableView cell row (contained in its tag ?) where the collection is : that should give an array of episodess

  • in this array, the episodess for the cellForItemAt indexPath.row

Hope that helps.
Thank you guys for your help i learn a lot problem solve

tons of thank you
Set an array of arrays inside of UICollectionView that's inside a UITableView and return the correct number of items?
 
 
Q