使用键存储数组并按条目顺序对它们进行排序的最佳方法
NSDictionary 和排序有一点问题。我在这里阅读了一些关于使用某些方法的帖子,例如 sortedArrayUsingSelector
和 keysSortedByValueUsingComparator
但无法完全掌握它。目前我有一个表视图控制器正确显示一些数据。我遇到的唯一问题是它显示的顺序(或者更确切地说,各部分的顺序)。
在声明文件中:
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字典不保证顺序,因此
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.