简体   繁体   English

NSMutableDictionary 的替代品?

[英]Alternatives to NSMutableDictionary?

I have 3 NSMutableDictionaries that hold data for 3 separate UITableViews.我有 3 个 NSMutableDictionaries 保存 3 个单独的 UITableViews 的数据。 When a value is checked from one of the UITableViews, I want to hold that value so depending on the different values that are checked from the different tables, I can generate an answer on the next page.当从一个 UITableViews 检查一个值时,我想保留该值,以便根据从不同表中检查的不同值,我可以在下一页上生成答案。 I thought maybe I could create a new NSMutableDictionary that has all the possible selections, and then when a user hits the checkbox, to tell my newNSMutableDictionary that that value has been selected.我想也许我可以创建一个包含所有可能选择的新 NSMutableDictionary,然后当用户点击复选框时,告诉我的 newNSMutableDictionary 该值已被选中。 But I don't think it works that way since it's a key-value-pairing.但我不认为它是这样工作的,因为它是一个键值对。 I was wondering if there were alternatives to this, or if someone had a good way of holding this information?我想知道是否有其他选择,或者是否有人有很好的方法来保存这些信息? Thanks.谢谢。

Instead of doing this, you can try having one NSMutableArray of the selected NSIndexPath objects.而不是这样做,您可以尝试使用一个NSMutableArray选定的NSIndexPath对象。 This way, you'd have a pretty lightweight memory footprint (lazy loading) and if you needed to grab the actual cell's value, you can ask the UITableView with -tableView:cellForRowAtIndexPath:这样,您将拥有一个非常轻量级的 memory 占用空间(延迟加载),如果您需要获取实际单元格的值,您可以使用 -tableView:cellForRowAtIndexPath 询问UITableView -tableView:cellForRowAtIndexPath:

Or, even better, have one NSMutableDictionary with one the keys being the tag of the specific tableview wrapped in an NSNumber and the value being an NSMutableArray of selected index paths.或者,更好的是,拥有一个NSMutableDictionary ,其中一个键是包装在NSNumber中的特定 tableview 的tag ,值是所选索引路径的NSMutableArray

To retrieve the selected index paths would be as simple as this:检索选定的索引路径就像这样简单:

NSArray *indexPaths = [selectedDict objectForKey:[NSNumber numberWithInt:tableView.tag]];

for(NSIndexPath *p in indexPaths) {
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:p];
   //do something with cell...
}

why not store all possible selections in a NSDictionary, initialise it with the all the selections a user can make.为什么不将所有可能的选择存储在 NSDictionary 中,用用户可以做出的所有选择对其进行初始化。 When the user makes a selection add that item to a "selectedItems" NSMutableDictionary.当用户进行选择时,将该项目添加到“selectedItems”NSMutableDictionary。 This way your NSMutableDictionary (that contains selections) will only ever have the selected items in it, you can then add and remove from this dictionary without needing to mutate the non-mutable NSDictionary that contains the selections that a user can make..?这样,您的 NSMutableDictionary (包含选择)将永远只包含选定的项目,然后您可以在此字典中添加和删除,而无需改变包含用户可以进行的选择的非可变 NSDictionary ..?

I'm not sure if I've understood your problem right.我不确定我是否正确理解了您的问题。

But, if your data has the "shape" of a tree you might consider to save the information in this way.但是,如果您的数据具有树的“形状”,您可能会考虑以这种方式保存信息。

  • You can put a Dictionary inside your Dictionary你可以在你的字典里面放一个字典
  • You can put your data in CoreData and use NSTreeController您可以将数据放入 CoreData 并使用 NSTreeController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM