NSDictionary allKeys命令

我需要在UITableView中显示API返回的NSDictionary的内容,尊重键的顺序。

我在用着 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *key = self.data.allKeys[indexPath.section]; NSArray *list = self.data[key]; id data = list[indexPath.row]; PSSearchCell *cell = [PSSearchCell newCellOrReuse:tableView]; cell.model = data; return cell; } 

但正如我做self.data.allKeys ,我失去了我的钥匙的顺序。 我不能按价值分类,因为它们不涉及它们。

尝试这个,

 NSArray *keys = [myDictionary allKeys]; keys = [keys sortedArrayUsingComparator:^(id a, id b) { return [a compare:b options:NSNumericSearch]; }]; NSLog(@"%@",keys); 

现在基于sorting的键值获取值。

编辑

按照字母顺序sorting,试试这个,

 NSArray *keys = [myDictionary allKeys]; keys = [[keys mutableCopy] sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; NSLog(@"%@",keys); 

正如大家所说, 我不能订购我的密钥,因为它们出现在我的NSDictionary日志中,因为NSDictionary没有sorting。

我要求API中的人员返回一个数组:

 0 => name : key 1 value : value 1 1 => name : key 2 value : value 2 

/ ————————————————- ————————— /

太糟糕了没有一种方法“sortedArrayUsingArray”使用像这样:

 NSArray * arrayOne = [@"key E", @"key A", @"key C"]; NSArray * arrayTwo = [@"key A", @"key B", @"key C", @"key D", @"key E", @"key F"]; NSArray * orderedArray = [arrayOne sortedArrayUsingArray: arrayTwo]; 

我写了一个快速的方法来获取一个源数组(全部无序的对象)和一个引用数组(它具有所需(完全任意)顺序的对象),并返回一个数组,其中源数组已被重新组织以匹配参考数组。

 - (NSArray *) reorderArray:(NSArray *)sourceArray toArray:(NSArray *)referenceArray { NSMutableArray *returnArray = [[NSMutableArray alloc] init]; for (int i = 0; i < [referenceArray count]; i++) { if ([sourceArray containsObject:[referenceArray objectAtIndex:i]]) { [returnArray addObject:[arrReference objectAtIndex:i]]; } } return [returnArray copy]; } 

请注意,这是非常脆弱的。 它使用NSArraycontainsObject:方法,最终将调用NSObjectisEqual: 基本上,它应该对NSString s, NSNumber s和NSDate s(还没有尝试过)的数组工作很好,但除此之外,YMMV。 我想如果你试图传递UITableViewCell的数组或其他一些非常复杂的对象,那么它本身就是完全的,并且会崩溃或者返回总的垃圾。 同样,如果你要做的事情就像传递一个NSDate的数组作为参考数组和一个NSString数组作为源数组。 另外,如果源数组包含引用数组中未包含的项目,则它们将被丢弃。 可以通过添加一些额外的代码来解决其中的一些问题。

所有这一切说,如果你想做一些简单的事情,它应该很好地工作。 在你的情况下,你只需发送arrayOne作为源数组, arrayTwo作为参考数组。

你说你失去了键的顺序,所以我想你从NSArray取出这些键。 对? 在那个NSArray中,您可以根据需要订购按键。 而且我也看到NSDictionary的对象是Arrays ,对吗? 是。 所以在你的Ordered Array你有更多的数组。

data是NSDictionary的名称。

所以,你所要做的就是把这个NSArray (按照你一直想要的顺序)放到这个.m文件中,然后在你的cellForRowAtIndexPath方法中使用一些非常棒的代码。 您可以通过执行以下操作来完成:

 @interface yourViewController () { NSArray *yourArrayOrdered; } @end @implementation yourViewController - (void)viewDidLoad { [super viewDidLoad]; yourArrayOrdered = [[NSArray alloc] initWith...:... ]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *key = yourArrayOrdered[indexPath.row]; NSArray *list = [self.data objectForKey:yourArrayOrdered[indexPath.row]]; //your code continues here... /*id data = list[indexPath.row]; //don't think you need this line PSSearchCell *cell = [PSSearchCell newCellOrReuse:tableView]; cell.model = data; */ return cell; } 

这就是所有你必须做的使用NSDictionary和NSArray保持你想要的顺序。

为了更好地显示它,在你的有序数组只包含string的情况下,它会是这样的:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *key = yourArrayOrdered[indexPath.row]; NSString *myString = [self.data objectForKey:yourArrayOrdered[indexPath.row]]; cell.textLabel.text = [NSString stringWithFormat:@"THIS IS THE OBJECT %@", myString]; cell.detailTextLabel.text = [NSString stringWithFormat:@"AND THIS IS THE KEY %@", key]; return cell; } 

希望能帮助到你。