【问题标题】:Nested UICollectionView returning the same values for each section - Swift嵌套的 UICollectionView 为每个部分返回相同的值 - Swift
【发布时间】:2020-04-13 00:34:31
【问题描述】:

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

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】:

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

    cell.theArray = network.sharedArray
    cell.accessArray = network.accessArray
    cell. collectionView.reloadData()
    

    【讨论】:

    • 是的!这有效!谢谢,我完全忘了重新加载collectionview。 :)
    • 如果这个答案对你有用,你可以点击左边的绿色箭头接受它
    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多