使用键存储数组并按条目顺序对它们进行排序的最佳方法

发布于 2024-12-20 18:52:47 字数 1436 浏览 3 评论 0原文

NSDictionary 和排序有一点问题。我在这里阅读了一些关于使用某些方法的帖子,例如 sortedArrayUsingSelectorkeysSortedByValueUsingComparator 但无法完全掌握它。目前我有一个表视图控制器正确显示一些数据。我遇到的唯一问题是它显示的顺序(或者更确切地说,各部分的顺序)。

在声明文件中:

@interface SettingsTableViewController : UITableViewController {
    NSDictionary *tableContents;
    NSArray *keys;
}
@property (nonatomic,retain) NSDictionary *data;
@property (nonatomic,retain) NSArray *keys;
@end

以及实现文件,对于 viewDidLoad

NSArray *arrayTemporary1 = [[NSArray alloc]initWithObjects:@"Settings 1",@"Setting 2",@"Setting 3",@"Setting 4",nil];
NSArray *arrayTemporary2 = [[NSArray alloc]initWithObjects:@"Enable Gestures",nil];
NSArray *arrayTemporary3 = [[NSArray alloc]initWithObjects:@"Instructions",@"Frequently Asked Questions",@"About",nil];

NSDictionary *temporary =[[NSDictionary alloc]initWithObjectsAndKeys:arrayTemporary1,@"Settings",arrayTemporary2,@"Application",arrayTemporary3,@"Information",nil];
self.tableContents = temporary;

self.keys = [self.tableContents allKeys];

[temporary release];
[arrayTemporary1 release];
[arrayTemporary2 release];
[arrayTemporary3 release];

从数组中访问单个单元格数据 NSArray *listData =[self.tableContents objectForKey:[self.keys objectAtIndex:[indexPath section] ]]];cellForRowAtIndexPath 方法中。

虽然这工作正常,但我遇到的问题是,它不是按照临时写入的顺序显示这些部分,而是随机显示。希望得到一些指导。

Having a little issue with NSDictionary and sorting. I've read a few posts on here about using certain methods such as sortedArrayUsingSelector and keysSortedByValueUsingComparator but cannot quite grasp it. At the moment I have a Table View Controller displaying some data correctly. The only issue I have is the order it is displaying it (or rather, the order of the sections).

In the declaration file:

@interface SettingsTableViewController : UITableViewController {
    NSDictionary *tableContents;
    NSArray *keys;
}
@property (nonatomic,retain) NSDictionary *data;
@property (nonatomic,retain) NSArray *keys;
@end

And the implementation file, for viewDidLoad:

NSArray *arrayTemporary1 = [[NSArray alloc]initWithObjects:@"Settings 1",@"Setting 2",@"Setting 3",@"Setting 4",nil];
NSArray *arrayTemporary2 = [[NSArray alloc]initWithObjects:@"Enable Gestures",nil];
NSArray *arrayTemporary3 = [[NSArray alloc]initWithObjects:@"Instructions",@"Frequently Asked Questions",@"About",nil];

NSDictionary *temporary =[[NSDictionary alloc]initWithObjectsAndKeys:arrayTemporary1,@"Settings",arrayTemporary2,@"Application",arrayTemporary3,@"Information",nil];
self.tableContents = temporary;

self.keys = [self.tableContents allKeys];

[temporary release];
[arrayTemporary1 release];
[arrayTemporary2 release];
[arrayTemporary3 release];

Individual cell data is accessed from an array NSArray *listData =[self.tableContents objectForKey:[self.keys objectAtIndex:[indexPath section]]]; inside the cellForRowAtIndexPath method.

Whilst this works fine, the issue I'm having is that rather than displaying the sections in the order that it is written in temporary, it does so randomly. Would appreciate some guidance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若能看破又如何 2024-12-27 18:52:47

字典不保证顺序,因此 tableContents 不记得向其中添加数组的顺序。您应该将有序数据保存在 NSArray 中。

将各部分的标题放在单独的 NSArray 中。然后在需要时使用您必须的字典查找单元格的数据。这使您可以按所需的顺序保留标题,并且仍然可以在字典中查找值。

Dictionaries do not guarantee ordering, so tableContents has no memory of the order in which you added the arrays to it. You should keep ordered data in NSArrays.

Put the titles of the sections in a separate NSArray. Then use the dictionary you have to look up the data for the cells when you need to. This lets you keep the titles in the order you want, and still have a way to find the values in the dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文