问题描述
我需要在UITableView中显示由API返回的NSDictionary的内容,尊重键的顺序。
I need to display in a UITableView the content of a NSDictionary returned by an API, respecting the order of the keys.
我正在使用:
- (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
,我正在丢失我的钥匙的顺序。我无法按价值对它们进行排序,因为它与它们无关。
but as I do self.data.allKeys
, I'm loosing the order of my keys. I can't sort them by value as it doesn't concern them.
推荐答案
正如大家所说,我可以因为NSDictionary日志中出现了我的密钥,因为没有订购NSDictionary。
As everyone said, I can't order my keys as they appear in my NSDictionary log because a NSDictionary is not ordered.
我要求API中的人返回一个数组:
I asked people from the API to return an array instead:
0 => name : key 1
value : value 1
1 => name : key 2
value : value 2
/ ----- -------------------------------------------------- --------------------- /
太糟糕了,没有方法 sortedArrayUsingArray就像这样使用:
Too bad there isn't a method "sortedArrayUsingArray" used like that :
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];
这篇关于NSDictionary allKeys订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!