对NSDictionary中的项目进行排序



我正在将plist中的项读取到dictionary中,我正在使用dictionary的键将title和value设置为按钮的标记。现在我必须对字典中的项目进行排序,我不想将排序后的项目放入数组中,因为我正在使用字典为按钮设置标题和标记。我检查了链接,但大多数都是将排序后的项目放入数组中。我怎么能在不使用数组的情况下做到这一点,而使用相同的字典呢。这是代码片段。

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)];
}

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

最新更新