Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从字典中按日期剖切tableview_Ios_Swift_Uitableview_Tableview_Swift Dictionary - Fatal编程技术网

Ios 从字典中按日期剖切tableview

Ios 从字典中按日期剖切tableview,ios,swift,uitableview,tableview,swift-dictionary,Ios,Swift,Uitableview,Tableview,Swift Dictionary,我有一个字典,它包含一个日期的键/值对,其中包含一个按相同日期分组的自定义对象数组 用餐对象: class Meal: NSObject, Codable { var id: String? var foodname: String? var quantity: Float! var brandName: String? var quantityType: String? var calories: Float! var date: Date? } 在我看来: var grouped = D

我有一个字典,它包含一个日期的键/值对,其中包含一个按相同日期分组的自定义对象数组

用餐对象:

class Meal: NSObject, Codable {

var id: String?
var foodname: String?
var quantity: Float!
var brandName: String?
var quantityType: String?
var calories: Float!
var date: Date?
}
在我看来:

var grouped = Dictionary<Date, [Meal]>()
var listOfAllMeals = [Meal]() //already populated

self.grouped = Dictionary(grouping: self.listOfAllMeals.sorted(by: { ($0.date ?? nilDate) < ($1.date ?? nilDate) }),
            by: { calendar.startOfDay(for: $0.date ?? nilDate) })

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return grouped.count
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return Array(grouped.keys)[section] as! String //this throws a thread error
}
var group=Dictionary()
var listofallfines=[fine]()//已填充
self.grouped=Dictionary(分组:self.listofallfines.sorted(按:{($0.date??nilDate)<($1.date??nilDate)}),
收件人:{calendar.startOfDay(for:$0.date??nilDate)})
重写func numberOfSections(在tableView:UITableView中)->Int{
//#警告未完成执行,返回节数
return.count
}
重写func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String?{
将数组(grouped.keys)[section]返回为!String//这会引发线程错误
}

这允许用户每天上传一顿饭多次,以备将来查看。现在,我想在一个表视图中显示这些饭,按日期分段,并按最新日期排序。如何实现这一点?

在您的数据模型中,您使用字典,每个条目使用一天,将键作为数组,并对数组进行排序

tableview的节数与该数组的条目数相同。您可以从日期开始创建每个部分的标题。对于每个部分,您从字典中获取膳食数组,因此每个部分都有不同数量的行,来自eac行的数据从数组中的一行获取


例如,要获取第3节第5行的数据,您可以从索引3中的日期数组中获取日期,在字典中查找日期并获取膳食数组,索引5中的膳食提供数据。

在数据模型中,您使用字典,每个条目有一天,将键作为数组,并对数组进行排序

tableview的节数与该数组的条目数相同。您可以从日期开始创建每个部分的标题。对于每个部分,您从字典中获取膳食数组,因此每个部分都有不同数量的行,来自eac行的数据从数组中的一行获取


例如,要获取第3节第5行的数据,可以从索引3中的日期数组中获取日期,在字典中查找日期并获取膳食数组,索引5中的膳食提供数据。

为各节创建一个结构

struct Section {
    let date : Date
    let meals : [Meal]
}
并将分组字典映射到
节的数组

var sections = [Section]()

let sortedDates = self.grouped.keys.sorted(>)
sections = sortedDates.map{Section(date: $0, meals: self.grouped[$0]!)}
您可以添加一个日期格式化程序来显示更有意义的
date
实例

表视图数据源方法是

override func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {        
    return sections[section].date.description
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections[section].meals.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "foodCell", for: indexPath)
    let meal = sections[indexPath.section].meals[indexPath.row]
    ...
注:


考虑使用较少的选项和结构,而不是
NSObject
子类。与
NSCoding
不同,
Codable
不要求符合
NSObjectProtocol
。并且从不将属性声明为隐式展开可选。

为节创建一个结构

struct Section {
    let date : Date
    let meals : [Meal]
}
并将分组字典映射到
节的数组

var sections = [Section]()

let sortedDates = self.grouped.keys.sorted(>)
sections = sortedDates.map{Section(date: $0, meals: self.grouped[$0]!)}
您可以添加一个日期格式化程序来显示更有意义的
date
实例

表视图数据源方法是

override func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {        
    return sections[section].date.description
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections[section].meals.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "foodCell", for: indexPath)
    let meal = sections[indexPath.section].meals[indexPath.row]
    ...
注:


考虑使用较少的选项和结构,而不是
NSObject
子类。与
NSCoding
不同,
Codable
不要求符合
NSObjectProtocol
。和从不将属性声明为隐式展开可选。

但是我如何从标题的特定索引访问键值(日期)?但是如何从标题的特定索引访问键值(日期)?有趣的是,您在这个问题中使用了我的答案,这是否意味着它对你有用?有趣的是,你在这个问题上使用了我的答案,这是否意味着它对你有用?