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") } }
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
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