【问题标题】:How to display row and section titles in collectionView based on Firestore data?如何根据 Firestore 数据在 collectionView 中显示行和节标题?
【发布时间】:2019-02-13 13:20:40
【问题描述】:

我正在尝试根据日期作为部分标题对 tableView 中的部分中的项目进行分类。我创建了两个数组,以便以后可以将它们合并为部分和行,例如:

var tableViewModel = [TableViewModel]()
var items = [TableViewModel]()

var sectionTableViewModel = [Timestamp]()
var sectionItems = [Timestamp]()

这是我从 Firestore 加载数据的方式:

ref.addSnapshotListener { documentSnapshot, error in
      guard let data = documentSnapshot else {
          print("Error fetching document: \(error!)")
          return
      }

      let documents = data.documents
      for document in documents {
          let item = TableViewModel(json: document.data())
          let sectionItem: Timestamp = document.data()["date"] as! Timestamp

           self.items.append(item)
          if (!self.sectionItems.contains(sectionItem)) {
              self.sectionItems.append(sectionItem)
          }
      }

      // Display as descending order.
      self.tableViewModel = self.items as [TableViewModel]
      self.sectionTableViewModel = self.sectionItems as [Timestamp]

      self.collectionView.reloadData()
  }

现在我不知道如何在 tableView 的部分下划分我的项目。到目前为止,我所做的是填充部分和行,例如:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.tableViewModel.count
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return self.sectionTableViewModel.count
}

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

    header.headerLabel.text = sectionTableViewModel[indexPath.section]
        return header
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! UpdatesCollectionViewCell

    cell.titleLabel.text =  self.tableViewModel[indexPath.row].title
}

它的作用是正确显示部分标题,但每个部分都一遍又一遍地显示所有数据,我不知道如何根据日期过滤数据。我是否创建另一个数组并将tableViewModelsectionTableViewModel 合并在一起?或者我是否过滤cellForItemAt 中的项目?还是我要更改 Firestore 数据的结构?

【问题讨论】:

    标签: ios arrays swift uicollectionview google-cloud-firestore


    【解决方案1】:

    您面临的问题是数据结构的问题。您有一组日期,可以定义您的部分,这很好。然后你有一个数组来定义你的行/项目,这也很好,但你需要的是每个日期/部分都有一个行/项目数组。

    你现在拥有的是你的所有部分都使用了同一个数组,这就是你的项目重复的原因。

    现在要解决这个问题,您可以在前端或从 Firestore 的后端做几件事。

    您可以在 Firestore 上创建一个数据结构,其中文档包含日期和数据数组。

    您可以做的另一件事是使用当前数据结构为每个日期创建一个新数组。要知道哪个数组属于哪个日期,Dictionary 就可以完成这项工作。

    现在至于我提供代码,或者直接给你解决方案,我不会这样做,因为这不是别人学习的方式。如果您有任何进一步的问题要澄清问题,我很乐意回答!

    【讨论】:

      猜你喜欢
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      相关资源
      最近更新 更多