嵌套的UICollectionView为每个节返回相同的值-Swift

问题描述 投票:0回答:1

我有一个嵌套在UITableViewCell内的UICollectionView。每个tableviewcell部分内部的collectionview应该根据该部分返回不同的数据。这是我的代码:

ViewController.swift

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

  var categoryKeys: [String]?

  let network = MediaNetworking()

  @IBOutlet weak var tableView: UITableView!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

// fetch data
    network.fetchMedia()

    tableView.delegate = self
    tableView.dataSource = self

    print("hello world")
  }


  func numberOfSections(in tableView: UITableView) -> Int {

    self.categoryKeys = network.categoryKeys

    return self.categoryKeys!.count
  }


  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
  }



  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SectionTableViewCell

    cell.theArray = network.sharedArray
    cell.accessArray = network.accessArray

    cell.backgroundColor = .purple


    return cell
  }


  func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    self.categoryKeys = network.categoryKeys

    return self.categoryKeys?[section]

  }


  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 300
  }

}

SectionTableViewCell.swift

class SectionTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

  var categoryKeys: [String]?
  var theArray: [String: [Entity]]?
  var accessArray: [Entity]?

  @IBOutlet weak var collectionView: UICollectionView!

  override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    collectionView.delegate = self
    collectionView.dataSource = self

  }

  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
  }


  func numberOfSections(in collectionView: UICollectionView) -> Int {

    return 1
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return accessArray!.count
  }

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MediaCollectionViewCell

    cell.artistLabel.text = accessArray![indexPath.row].name

    return cell

  }


  func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if kind == UICollectionView.elementKindSectionHeader {
      let sectionHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SectionHeaderView", for: indexPath) as! SectionHeaderView

      if indexPath.section < categoryKeys!.count {

        let category = categoryKeys![indexPath.section]
        sectionHeader.categoryLabel.text = category
      }
      return sectionHeader
    }

    return UICollectionReusableView()
  }


  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: 400, height: 300)
  }

}

现在所有collectionview单元格都返回相同的值。关于我可能会缺少的任何想法吗?。

ios swift uitableview multidimensional-array uicollectionview
1个回答
0
投票

您是否在更改数据数组后尝试调用collectionView.reloadData()?

cell.theArray = network.sharedArray
cell.accessArray = network.accessArray
cell. collectionView.reloadData()
© www.soinside.com 2019 - 2024. All rights reserved.