Reorder dynamic UITableView by NSDate

我只是一个虾纸丫 提交于 2020-01-06 23:50:23

问题


In my app I have a UITableView which will be fill with core date items.

As you can see in this post, I have already asked for a sorting function: https://stackoverflow.com/a/36703648/6012187

func ReSort() {
   CoreDataItems.sortInPlace({sortAscending ? $0.RowName < $1.RowName : $0.RowName > $1.RowName})
   sortAscending = !sortAscending
   tableView.reloadData()
}

Now my question is, how can I set dynamically the RowName Value? for example. I save the the RowName in the user defaults.

but $0.defaults.objectForKey("xx") doesn't work without syntax error

Next question is:

How can I sort with a row, which value is a NSDate?


回答1:


You can sort NSArray which contains NSDate by using this code:

// objective-c code
NSSortDescriptor *dateDescriptor = [NSSortDescriptor
                             sortDescriptorWithKey:@"startDateTime" 
                                         ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [nodeEventArray
 sortedArrayUsingDescriptors:sortDescriptors];

//swift code
let dateDescriptor :NSSortDescriptor = NSSortDescriptor(key: "startDateTime", ascending: true)
let sortdescriptors: NSArray = [dateDescriptor]
let sortedeventarray = nodeEventArray.sortedArrayUsingDescriptors([dateDescriptor])

After sorting is done, you can reload your tableview



来源:https://stackoverflow.com/questions/36710670/reorder-dynamic-uitableview-by-nsdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!