【问题标题】:Sort table data in descending order Objective C按降序对表数据进行排序 Objective C
【发布时间】:2017-06-12 06:54:29
【问题描述】:

我正在使用NSSortDescriptor 对存储在NSArray 中的值进行降序排序,排序后的值将显示在控制台中。

但是重新加载表格视图时,zhr排序的表格数据没有排序。

这是我的代码:

NSArray *descriptor = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"Priority"  ascending:NO]];
self.searchResult = [self.responseData sortedArrayUsingDescriptors:descriptor];
NSLog(@"Sorted Array %@", self.searchResult);

【问题讨论】:

  • 添加tableview数据源方法。
  • 你能展示你的 dataSource 方法吗?您应该在那里使用 searchResult 。并确保在对数组进行排序后调用 [tableView reloadData]
  • 是的,我重新加载了 tableview。 [self.taskTableView reloadData];
  • 我已经包含了我的 dataSource 方法。 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (self.searchResult) { return [self.searchResult count]; } else { 返回 [self.taskDate 计数]; } }

标签: ios objective-c nsarray nssortdescriptor


【解决方案1】:

试试这个代码:

NSArray *SortedData = [yourDict or Array allKeys];
        NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
       SortedData = [SortedData sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

【讨论】:

  • yourDict 是字典还是数组?
  • 这是一个临时数组,您必须将要排序的数组或字典的值放入其中
【解决方案2】:

试试这个:

 NSSortDescriptor *brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Priority" ascending:NO];
 NSArray *sortDescriptors = [NSArray arrayWithObject:brandDescriptor];
 NSArray *sortedArray = [tripStatusArray sortedArrayUsingDescriptors:sortDescriptors];
 [yourTableView reloadData];

使用sortedArray 在您的tableView 上加载数据。希望这会有所帮助。

【讨论】:

  • 我试过这个方法,但它没有加载排序后的表数据
  • @SherineSJ 在这个方法之后你重新加载表视图了吗?如果不检查我编辑的答案
  • 是的,我重新加载了 tableView。我也试过你编辑的答案,它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
相关资源
最近更新 更多