通过 NSDate 重新排序动态 UITableView

Reorder dynamic UITableView by NSDate

在我的应用程序中,我有一个 UITableView,它将填充核心日期项。

正如你在这个post中看到的,我已经要求了一个排序功能:

func ReSort() {
   CoreDataItems.sortInPlace({sortAscending ? [=10=].RowName < .RowName : [=10=].RowName > .RowName})
   sortAscending = !sortAscending
   tableView.reloadData()
}

现在我的问题是,如何动态设置 RowName 值? 例如。我将 RowName 保存在用户默认值中。

但是 [=11=].defaults.objectForKey("xx") 没有语法错误就无法工作

下一个问题是:

如何对一行进行排序,哪个值是 NSDate?

您可以使用以下代码对包含 NSDate 的 NSArray 进行排序:

// 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])

排序完成后,您可以重新加载您的表格视图