【发布时间】: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
}
它的作用是正确显示部分标题,但每个部分都一遍又一遍地显示所有数据,我不知道如何根据日期过滤数据。我是否创建另一个数组并将tableViewModel 和sectionTableViewModel 合并在一起?或者我是否过滤cellForItemAt 中的项目?还是我要更改 Firestore 数据的结构?
【问题讨论】:
标签: ios arrays swift uicollectionview google-cloud-firestore