在NSDictionary中对项目进行排序

时间:2014-04-22 11:24:37

标签: nsdictionary

我正在读取plist中的项目到字典中,我使用字典键将标题和值设置为按钮的标记。现在我必须对字典中的项目进行排序,我不想将已排序的项目放入数组,因为我使用字典来设置按钮的标题和标记。我检查了链接,但大多数都是将已排序的项目放入数组。我如何使用数组,而不是使用相同的字典。 这是代码片段。

while (key = [keyEnumerator nextObject])
{
    UIButton *btnWith = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnWith.tag = [[topics objectForKey:key] integerValue] ;
    [btnWith setTitle:key forState:UIControlStateNormal];
    btnWith.titleLabel.font = [UIFont fontWithName:@"Aller-Light" size:15];
    [btnWith setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btnWith.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btnWith setBackgroundImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
    [btnWith addTarget:self action:@selector(CheckboxClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollview setContentSize: CGSizeMake(320, topics.count *50)];

}

1 个答案:

答案 0 :(得分:0)

未定义字典条目的顺序。您必须对键进行排序(例如,使用任何sortXXX methods)并将它们保存在有序的集合中(例如,数组)。 然后使用此集合以所需顺序访问字典条目。