在 table 视图中显示一组日期,Swift
Display an array of dates in table view, Swift
搜索SO后,发现tableview controller中关于数据分组和显示的问题很多,但是none需要按日期排序。
我有一个日期对象数组
let dateObjects = [2016-12-22 16:00:00 +0000, 2016-12-22 16:15:00 +0000, 2016-12-23 16:00:00 +0000, 2016-12-23 16:15:00 +0000, 2016-12-23 16:30:00 +0000, 2016-12-24 16:00:00 +0000]
如何从上面的 dateObjects 创建如下所示的数组。
let items = [["4:00 pm", "4:15 pm"], ["4:00 pm", "4:15 pm", "4:30 pm"], ["4:00 pm"]]
我希望将日期分组并显示在 table 视图中,如下图所示。请注意,日期和时间按升序排列。
我试图在字典中对日期进行分组,发现字典没有排序,因此我不会得到我想要的顺序。
当我提供一组项目时,此代码有效。 但是我如何从日期对象中创建类似项目的数组?
let section = ["Thu, 22 Dec", "Fri, 23 Dec", "Sat, 24 Dec"]
let items = [["4:00 pm", "4:15 pm"], ["4:00 pm", "4:15 pm", "4:30 pm"], ["4:00 pm"]]
override func numberOfSections(in tableView: UITableView) -> Int {
return self.section.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
cell.textLabel?.text = self.items[indexPath.section][indexPath.row]
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.section[section]
}
var valueToPass:String!
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath){
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
valueToPass = currentCell.textLabel?.text
performSegue(withIdentifier: "TableDetailSegue", sender: self)
}
我不明白问题是什么。你说:
I want dates to be grouped and shown in a table view like the below picture. Note that the dates and times are in ascending order.
I've tried to group the dates in a dictionary to realise that the dictionaries are not ordered and therefore I would not get the order I want.
好的,但是你展示了你的代码,没有字典 任何地方都看不到!相反,您有一个数组和一个数组数组。看起来应该可以。果然,我将你的代码复制到一个项目中 运行 它,这就是我得到的:
嗯,这看起来很像你说的你想要的。 (除了事物的高度;你没有给出那部分代码。但它是正确分组并按正确顺序排列的正确数据。)所以在我看来,你的代码可以提供所需的结果。因此很难看出你的问题是什么。
搜索SO后,发现tableview controller中关于数据分组和显示的问题很多,但是none需要按日期排序。
我有一个日期对象数组
let dateObjects = [2016-12-22 16:00:00 +0000, 2016-12-22 16:15:00 +0000, 2016-12-23 16:00:00 +0000, 2016-12-23 16:15:00 +0000, 2016-12-23 16:30:00 +0000, 2016-12-24 16:00:00 +0000]
如何从上面的 dateObjects 创建如下所示的数组。
let items = [["4:00 pm", "4:15 pm"], ["4:00 pm", "4:15 pm", "4:30 pm"], ["4:00 pm"]]
我希望将日期分组并显示在 table 视图中,如下图所示。请注意,日期和时间按升序排列。
我试图在字典中对日期进行分组,发现字典没有排序,因此我不会得到我想要的顺序。
当我提供一组项目时,此代码有效。 但是我如何从日期对象中创建类似项目的数组?
let section = ["Thu, 22 Dec", "Fri, 23 Dec", "Sat, 24 Dec"]
let items = [["4:00 pm", "4:15 pm"], ["4:00 pm", "4:15 pm", "4:30 pm"], ["4:00 pm"]]
override func numberOfSections(in tableView: UITableView) -> Int {
return self.section.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
cell.textLabel?.text = self.items[indexPath.section][indexPath.row]
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.section[section]
}
var valueToPass:String!
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath){
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
valueToPass = currentCell.textLabel?.text
performSegue(withIdentifier: "TableDetailSegue", sender: self)
}
我不明白问题是什么。你说:
I want dates to be grouped and shown in a table view like the below picture. Note that the dates and times are in ascending order.
I've tried to group the dates in a dictionary to realise that the dictionaries are not ordered and therefore I would not get the order I want.
好的,但是你展示了你的代码,没有字典 任何地方都看不到!相反,您有一个数组和一个数组数组。看起来应该可以。果然,我将你的代码复制到一个项目中 运行 它,这就是我得到的:
嗯,这看起来很像你说的你想要的。 (除了事物的高度;你没有给出那部分代码。但它是正确分组并按正确顺序排列的正确数据。)所以在我看来,你的代码可以提供所需的结果。因此很难看出你的问题是什么。