【问题标题】:How to sort json data and display in a tableView as alphabetical order [duplicate]如何对json数据进行排序并按字母顺序显示在tableView中[重复]
【发布时间】:2018-01-12 09:21:39
【问题描述】:

我正在实现一个需要显示 tableView 的应用程序。 tableView 需要显示 json 数据并通过单击它需要显示以该字母开头的数据的任何字母在右侧进行字母搜索。我已经实现了垂直字母搜索,当用户单击任何字母时,它会将用户带到该特定部分。但问题是我的 json 数据没有得到排序。

这是我的代码。 // 这里的existorArray 包含了在tableView 中填充数据的所有信息。

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"serverData" ascending:YES];
NSArray *sortedArray = [serverData sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
NSLog(@"Sorted Array:%@",sortedArray);

【问题讨论】:

  • 您需要显示数组的至少一个元素(或serverData 的结构),并给我们数组必须排序的键。
  • 我无法理解您的意思,请您详细说明一下
  • serverData 是什么?它看起来像什么?
  • 你能打印出来并作为你的数组值吗?
  • serverData 是来自后端的数组,必须按字母顺序排序并显示在表格视图中

标签: ios objective-c xcode8


【解决方案1】:
NSArray *unsortedStrings = @[@"Lamborghini", @"Lamborghini", @"InStyle",@"HSBC",@"Heineken"];
NSLog (@"unsorted = %@",unsortedStrings);

NSArray *sortedStrings =[unsortedStrings sortedArrayUsingSelector:@selector(compare:)];

NSLog (@"sorted = %@",sortedStrings);

未排序 = ( 兰博基尼, 兰博基尼, 很有型, 汇丰银行, 喜力 )

排序 = ( 汇丰银行, 喜力, 很有型, 兰博基尼, 兰博基尼 )

【讨论】:

  • 现在我的问题是当我点击一个字母时(当我点击 R 时)然后它必须被移动到列表中的特定字符串????
  • NSIndexSet *indexes = [sortedStrings indexOfObjectsPassingTest:^BOOL(NSString *string, NSUInteger idx, BOOL *stop) { return [string hasPrefix:@"L"]; }];并将您的表格滚动到 -> [indexes firstIndex]
  • 我是否必须为每个字母写这个代码,(我的意思是所有 26 个字母)???
  • 只是稍后找到录音并在此语法的后期传递,您将获得后者的索引并在该索引上滚动您的表格。
  • 我很了解你。你能详细说明一下吗.....
猜你喜欢
  • 2012-12-06
  • 2014-05-14
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 2018-12-07
相关资源
最近更新 更多