如何解决在 UITableView 内的 UICollectionView 内设置一个数组数组并返回正确数量的项目?
我正在尝试使用 UICollectionView 获取 UITableView,以在 UICollectionViews 中正确显示以下数据集。 包含所有这些信息的数组是
arregloDeArreglos
这是数据
▿ 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.jpg"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 1 : episodess
▿ episode_number : Optional<Int>
- some : 2
▿ name : Optional<String>
- some : "131045"
▿ still_path : Optional<String>
- some : "/mSZDdQLHnDousFg35ndlmv42908.jpg"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 2 : episodess
▿ episode_number : Optional<Int>
- some : 3
▿ name : Optional<String>
- some : "181045"
▿ still_path : Optional<String>
- some : "/57bidANnv5zdPmJckmPL9f4Ogb7.jpg"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 3 : episodess
▿ episode_number : Optional<Int>
- some : 4
▿ name : Optional<String>
- some : "291045"
▿ still_path : Optional<String>
- some : "/swmIOzEJ8uIjCdd5B4w9Ln5qXsJ.jpg"
▿ 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.jpg"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 6 : episodess
▿ episode_number : Optional<Int>
- some : 7
▿ name : Optional<String>
- some : "011145"
▿ still_path : Optional<String>
- some : "/8LGArTKq5oMnKR9AjdRd3ocxJWu.jpg"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 7 : episodess
▿ episode_number : Optional<Int>
- some : 8
▿ name : Optional<String>
- some : "021145"
▿ still_path : Optional<String>
- some : "/yEBAannVEt2QhyUKyjlXhXrEVld.jpg"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 8 : episodess
▿ episode_number : Optional<Int>
- some : 9
▿ name : Optional<String>
- some : "031145"
▿ still_path : Optional<String>
- some : "/nf1zvR7D4kN1Xa1wlxXeuUrWpOo.jpg"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 9 : episodess
▿ episode_number : Optional<Int>
- some : 10
▿ name : Optional<String>
- some : "130146"
▿ still_path : Optional<String>
- some : "/1lTlSWY4chqpv1ibeQz4eYHaePf.jpg"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 10 : episodess
▿ episode_number : Optional<Int>
- some : 11
▿ name : Optional<String>
- some : "140146"
▿ still_path : Optional<String>
- some : "/qTKxjZTbiCqZ6lywwfmfTLX8QnK.jpg"
▿ seasonNumerEpisode : Optional<Int>
- some : 1
▿ 11 : episodess
▿ episode_number : Optional<Int>
- some : 12
▿ name : Optional<String>
- some : "150146"
▿ still_path : Optional<String>
- some : "/8duv3ltJa7Olb721zsqcbss0kVx.jpg"
▿ 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.jpg"
▿ 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.jpg"
▿ 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.jpg"
▿ 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.jpg"
▿ 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.jpg"
▿ 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.jpg"
▿ 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.jpg"
▿ 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.jpg"
▿ 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
但是,当我在 UICollectionViews 中水平滚动时,数据会变得一团糟。
正如你在两张图片中看到的
这是我的代码
表委托和数据源
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 委托和数据源
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,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
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,])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。