如何按日期对我的表视图中的数组进行排序
How to sort my array in my tableview by Date
我正在尝试 return 以下单元格按日期排序。我搜索了很多帖子,但我真的不明白我具体把排序放在哪里:
let sortedArray = jogs.sorted { [=10=].jogDate < .jogDate }
这是我在 TableViewController 中使用的函数:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCellIdentifier") as? ToDoCell else {
fatalError("Could not dequeue a cell")
}
cell.delegate = self
let jog = jogs[indexPath.row]
cell.titleLabel?.text = jog.title
cell.descriptionLabel.text = jog.notes
cell.dateLabel.text = Jog.jogDateFormatter.string(from: jog.jogDate)
return cell
}
或者我应该把排序放在结构文件中吗?另外 Class 文件会比 Struct 文件更好吗?
1.按日期
对您的慢跑进行排序
override func viewDidLoad() {
super.viewDidLoad()\
jogs.sort { [=10=].jogDate < .jogDate }
tableView.reloadData()
}
或
2。在 UITableViewDelegate / Datasource implements
中使用排序数组
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCellIdentifier") as? ToDoCell else {
fatalError("Could not dequeue a cell")
}
cell.delegate = self
let jog = sortedArray[indexPath.row] // sorted array
cell.titleLabel?.text = jog.title
cell.descriptionLabel.text = jog.notes
cell.dateLabel.text = Jog.jogDateFormatter.string(from: jog.jogDate)
return cell
}
我正在尝试 return 以下单元格按日期排序。我搜索了很多帖子,但我真的不明白我具体把排序放在哪里:
let sortedArray = jogs.sorted { [=10=].jogDate < .jogDate }
这是我在 TableViewController 中使用的函数:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCellIdentifier") as? ToDoCell else {
fatalError("Could not dequeue a cell")
}
cell.delegate = self
let jog = jogs[indexPath.row]
cell.titleLabel?.text = jog.title
cell.descriptionLabel.text = jog.notes
cell.dateLabel.text = Jog.jogDateFormatter.string(from: jog.jogDate)
return cell
}
或者我应该把排序放在结构文件中吗?另外 Class 文件会比 Struct 文件更好吗?
1.按日期
对您的慢跑进行排序override func viewDidLoad() {
super.viewDidLoad()\
jogs.sort { [=10=].jogDate < .jogDate }
tableView.reloadData()
}
或
2。在 UITableViewDelegate / Datasource implements
中使用排序数组override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCellIdentifier") as? ToDoCell else {
fatalError("Could not dequeue a cell")
}
cell.delegate = self
let jog = sortedArray[indexPath.row] // sorted array
cell.titleLabel?.text = jog.title
cell.descriptionLabel.text = jog.notes
cell.dateLabel.text = Jog.jogDateFormatter.string(from: jog.jogDate)
return cell
}