swift - 如何在 swift 中根据日期升序对 Tableview 行进行排序
问题描述
我有 eventsTableView .. 无论日期是什么,这里所有行都一一添加.. 但在这里我需要根据其日期显示 tablevieworderedAscending
总代码是:
class EventsViewController: UIViewController {
var eventList : EventsModel? = nil
@IBOutlet weak var eventsTableView: UITableView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
getAllEventsList()
}
func getAllEventsList() {
//URLs and code..
do {
let jsonObject = try JSONSerialization.jsonObject(with: data!, options: .mutableLeaves) as! [String :AnyObject]
print("publish event \(jsonObject)")
self.eventList = EventsModel.init(fromDictionary: jsonObject)
DispatchQueue.main.async {
if self.eventList?.events.count != 0 {
DispatchQueue.main.async {
self.eventsTableView.reloadData()
}
}
else {
DispatchQueue.main.async {
Constants.showAlertView(alertViewTitle: "", Message: "No Events \(self.eventType)", on: self)
self.eventList?.events.removeAll()
self.eventsTableView.reloadData()
}
}
dataTask.resume()
}
}
extension EventsViewController : UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return eventList?.events.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: EventsTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EventsTableViewCell") as! EventsTableViewCell
let event = eventList!.events[indexPath.row]
if event.isAllDayEvent == true{
cell.eventDate.text = event.eventDate
cell.nameLbl.text = event.eventName
}
else{
cell.cancelLbl.text = ""
cell.nameLbl.text = event.eventName
cell.eventDate.text = event.eventDate
return cell
}
}
这是 EventsModel 代码: 就像我们创建的模型一样.. 如何从这里排序日期
class EventsModel : NSObject, NSCoding {
var events : [EventsModelEvent]!
init(fromDictionary dictionary: [String:Any]){
events = [EventsModelEvent]()
if let eventsArray = dictionary["Events"] as? [[String:Any]]{
for dic in eventsArray{
let value = EventsModelEvent(fromDictionary: dic)
events.append(value)
}
}
}
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if events != nil{
var dictionaryElements = [[String:Any]]()
for eventsElement in events {
dictionaryElements.append(eventsElement.toDictionary())
}
dictionary["events"] = dictionaryElements
}
return dictionary
}
这是事件模型事件
class EventsModelEvent : NSObject, NSCoding {
var eventName : String!
var eventDate: string!
init(fromDictionary dictionary: [String:Any]){
eventName = dictionary["eventName"] as? String
eventDate = dictionary["eventDate"] as? String
}
}
请帮助我以日期升序显示 tableview 行。
解决方案
您需要在重新加载 tableview 之前根据日期对事件数组进行排序。你在这样做时面临的挑战是什么?
推荐阅读
- flutter - 如何在行和列中制作 6 个 RisedButton | 颤动与飞镖
- typescript - typescript declare:`export function`和`export type .. lambda`有什么区别?
- c# - C#请求路径与路由器路径比较
- angular - 客户端未在信号器中接收消息
- javascript - 反应复选框未正确呈现
- azure - [错误] Bash 以代码“1”退出 - Azure YAML 管道中的替换错误
- flutter - //没有为类型'Object'定义运算符'[]'。?
- ios - 核心数据自定义迁移策略
- node.js - Nodejs Typescript 构建返回错误:找不到模块“。”
- android - 如何修复我的 nativescript 应用程序中的 build.gradle 错误