如何在标题 Collection View 中对从 firebase 收到的数据进行分组?
从 firebase 我收到了包含以下数据的array:
{
"N7AooUYQU576hb5qLux" : {
"requested_date" : 20190110,
"requested_times" : {
0: 1,
1: 2,
}
"0ckbfkwm2yR0wbcEQ2XT" : {
"requested_date" : 20190110,
"requested_times" : {
0: 3,
1: 4,
}
"38kBVw01kvJtYTtYt0ba" : {
"requested_date" : 20190211,
"requested_times" : {
0: 5,
1: 6,
}
"3bQ3WTwasALxqNNR9P4c" : {
"requested_date" : 20190315,
"requested_times" : {
0: 1,
1: 2,
}
"51OhvSiBGDa0HH8WV5bt" : {
"requested_date" : 20190211,
"requested_times" : {
0: 10,
1: 11,
}
}
要从 firestore 检索数据,我使用以下代码:
var bookingHall: [BookingHall] = []
var document: [DocumentSnapshot] = []
fileprivate func observeQuery() {
guard let query = query else { return }
listener = query.addSnapshotListener { [unowned self] (snapshot, error) in
if let err = error {
self.unknownError(error: err)
} else {
if let snapshot = snapshot {
let bookingModel = snapshot.documents.map { (document) -> BookingHall in
if let newBooking = BookingHall(dictionary: document.data()) {
return newBooking
} else {
fatalError("Fatal error")
}
}
self.bookingHall = bookingModel
self.document = snapshot.documents
self.collectionView.reloadData()
}
}
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return ...
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.bookingHall.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "studioBookCollCell", for: indexPath) as! StudioBookCollectionCell
let timeStart = self.bookingHall[indexPath.item].requestedTimes.first!
let timeEnd = self.bookingHall[indexPath.item].requestedTimes.last! + 1
cell.timeLabel.text = String(format: "%02d:00 - %02d:00", timeStart, timeEnd)
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerBook", for: indexPath) as! StudioBookCollectionReusableView
header.dateLabel.text = ""
return header
}
我希望看到的最终结果就像下面的图片:
我不知道如何对我的数据进行分组并应用于标题,并且每个标题的单元格中都有正确的数据。
请告诉我怎么做?如果需要更多代码,我会更新我的帖子。
我的结构:
protocol BookingDocumentSerializable {
init?(dictionary:[String:Any])
}
struct BookingHall {
var contactInfo: [String: Any] = [:]
var creationDate: Timestamp
var requestedTimes: [Int] = []
var uid: String = ""
var hall: String = ""
var requestedDate: Int = 0
var dictionary: [String: Any] {
return [
"contact_info": contactInfo,
"creation_date": creationDate,
"requested_times": requestedTimes,
"uid": uid,
"hall": hall,
"requested_date": requestedDate
]
}
}
extension BookingHall: BookingDocumentSerializable {
init?(dictionary: [String: Any]) {
let contactInfo = dictionary["contact_info"] as? [String: Any] ?? [:]
let creationDate = dictionary["creation_date"] as? Timestamp
let requestedTimes = dictionary["requested_times"] as? [Int] ?? []
let uid = dictionary["uid"] as? String ?? ""
let hall = dictionary["hall"] as? String ?? ""
let requestedDate = dictionary["requested_date"] as? Int ?? 0
self.init(contactInfo: contactInfo,
creationDate: creationDate!,
requestedTimes: requestedTimes,
uid: uid,
hall: hall,
requestedDate: requestedDate)
}
}
最佳答案
您可以使用分组
来实现您的要求。
Creates a new dictionary whose keys are the groupings returned by the given closure and whose values are arrays of the elements that returned each key.
请引用下面的代码,这是一个如何实现上述要求的想法,我请求请忽略其他语法错误。
let aryData = [BookingHall]() // Your main array
//Create dicationary with grouped value with `requested_date`
let dict = Dictionary(grouping: aryData, by: { $0.requested_date })
//Format Array for populate data into UITableView
let allKeys = Array(dict.keys)
let aryFinalData = [FinalData]()
for value in allKeys{
let data = FinalData(title: value, aryData: dict[value]!)
aryFinalData.append(data)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return aryFinalData.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return aryFinalData[section].aryData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellData = aryFinalData[indexPath.section].aryData[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerBook", for: indexPath) as! StudioBookCollectionReusableView
let headerData = aryFinalData[indexPath.section]
header.dateLabel.text = headerData.requested_date
return header
}
为标题和子数组创建新结构
struct FinalData{
let title:String?
let aryData:[BookingHall]?
}
关于ios - 标题 Collection View 中的分组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55990308/