【发布时间】:2020-08-26 17:22:01
【问题描述】:
我有 eventsTableView.. 无论日期是什么,这里所有行都一一添加.. 但这里我需要根据其日期显示 tableview orderedAscending
总代码为:
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
}
这是 EventsModelEvent
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
}
}
请帮我以日期升序显示表格视图行。
【问题讨论】:
-
什么是
EventsModel?它是结构还是类?events是什么?eventDate的格式是什么?您需要按日期sort事件数组。解决方案取决于这些问题的答案。 -
@vadian 谢谢.. 用
EventsModel和EventsModelEvent编辑了我的帖子 -
@vadian.. 如何对
EventsModel中的日期进行排序 -
您是否要按日期对
EventsModelEvent进行排序?如果是这样,它将需要符合Comparable协议,以便 Swift 知道如何对其进行排序。这可能意味着您需要将日期属性从String转换为Date,至少暂时用于排序,因此它会按照您需要的方式进行排序。
标签: swift date uitableview