iPhone - NSDray到NSArray保留订单

qua*_*ano 1 iphone cocoa cocoa-touch nsdictionary nsarray

我在一个属性列表中有几个词典,它构成了我游戏的菜单系统,如下所示:

New game
    Easy
    Normal
    Hard
Load game
    Recent
    Older
Options
    Game
    Sound
Run Code Online (Sandbox Code Playgroud)

等等.此列表中的每个项目都是字典.

我使用UINavigationController和UITableViews来显示这个菜单系统.选择项目后,UINavigationController中将推送一个新的UITableViewController以及所选项目的项目.例如,第一个UITableView在其字典中包含"新游戏","加载游戏"和"选项".如果用户选择选项,则创建具有"Game"和"Sound"项(即"Options"的字典)的新UITableViewController.

我用这种方式填充UITableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] init] autorelease];
    }

    // Set up the cell...

    cell.text = [[[data keyEnumerator] allObjects] objectAtIndex:indexPath.row];

    // Data is the dictionary..

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

但显然,这会导致项目与属性列表中定义的顺序不同.

在做我想做的事情时,有谁知道如何保留订单?

谢谢.

Sea*_*ess 5

字典永远不会保留秩序.你需要使用一个数组.您可以使用数组(我没有看到您需要示例中的字典的任何原因),或者您可以创建一个索引字典的数组.

因此,从现在的字典开始,创建一个数组,遍历字典并将数组值设置为字典中的键.

所以如果你的字典看起来像这样

Game Types
    easy: Easy
    normal: Normal
    hard: Hard
Run Code Online (Sandbox Code Playgroud)

你的数组看起来像这样:["easy","normal","hard"](这些都不是客观的c代码).然后,您可以使用以下行.

[myDictionary objectForKey:[myArray objectAtIndex: indexPath.row]]
Run Code Online (Sandbox Code Playgroud)