first of all what i'm only trying to achieve is the scroll horizontally.
Heres my UITableView declaration on the View:
Code Block let tableSeasons: UITableView = { let table = UITableView(frame: CGRect.zero) table.register(NumberOfSeasonsCell.self, forCellReuseIdentifier: "seasonsRegister") table.backgroundColor = .clear table.layer.cornerRadius = 0 table.separatorColor = .clear table.translatesAutoresizingMaskIntoConstraints = false table.showsHorizontalScrollIndicator = false table.showsVerticalScrollIndicator = false return table }()
here's the Delegate and DataSource protocols in VC:
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.backgroundColor = .clear cell.selectionStyle = .none cell.seasonsCollectionView.delegate = self cell.seasonsCollectionView.dataSource = self return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 200 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { } }
This is the code for UICollectionView declaration on the NumberOfSeasonsCell or UITableViewCell
Code Block lazy var seasonsCollectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .horizontal layout.minimumLineSpacing = 15 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 }()
Protocols for the UICollectionView
Code Block extension NumberOfSeasonsCell: 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 cell.model = arraySeasonsEpisodes[indexPath.item] return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 150, height: 100) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 15 } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print(indexPath.row) } }
i've read that i should store the last position of the Table for scrolling the UICollectionView but i've tried but not work for me
here's the UICollectionViewCell
Code Block class SeasonCell: UICollectionViewCell { var model: episodess? { didSet { guard let viewModel = model else { return } let guardado = viewModel.still_path let number = String(viewModel.episode_number ?? 0) episodeNumber.text = "Episode Number: \(number)" episodeName.text = "Episode Name: \(viewModel.name ?? "")" let image = "500\(guardado ?? "")" if image == "" || image == "undefined" { seasonImage.image = UIImage(named: "") 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") } }
adding this extension works
Code Block extension UITableViewCell { open override func addSubview(_ view: UIView) { super.addSubview(view) sendSubviewToBack(contentView) } }