首页 > 解决方案 > 如何在 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 行。

标签: swiftdateuitableview

解决方案


您需要在重新加载 tableview 之前根据日期对事件数组进行排序。你在这样做时面临的挑战是什么?


推荐阅读