【发布时间】:2016-12-22 19:55:52
【问题描述】:
搜索SO后,我发现有很多关于在table view controller中分组和显示数据的问题,但没有一个需要按日期排序。
我有一个日期对象数组
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"]]
我希望将日期分组并显示在如下图所示的表格视图中。请注意,日期和时间按升序排列。
我尝试将字典中的日期分组以意识到字典没有排序,因此我不会得到我想要的顺序。
当我提供一组项目时,此代码有效。 但是如何从日期对象创建类似项目的数组?
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)
}
【问题讨论】:
-
如果你想要有序的日期,你需要创建一个 array ,其中分别包含子数组中的日期和时间。数组表示节和节中次 的子数组。最合适的方法是自定义结构或类,包括将日期格式化为正确字符串的属性。无论如何不推荐
NSDictionary。 -
我不禁注意到您没有显示任何代码。没有任何。零。齐尔奇。纳达。那么,当你根本不展示你在做什么的时候,怎么会有人说你做错了什么?
-
你的意思是这样的吗?
['Thu 22 Dec' : ['4:00 pm', '4:15 pm'], 'Fri 23 Dec': ['4:00 pm', '4:15 pm', '4: 30 pm'], 'Sat 24 Dec' : ['4:00 pm']] -
@matt,添加了我的代码。
-
不知道为什么它被否决..
标签: swift uitableview swift3 tableview nsdictionary