NSMutableDictionary 的替代品?
我有 3 个 NSMutableDictionaries,它们保存 3 个单独的 UITableView 的数据。当从一个 UITableViews 检查一个值时,我想保留该值,因此根据从不同表检查的不同值,我可以在下一页上生成答案。我想也许我可以创建一个新的 NSMutableDictionary 来包含所有可能的选择,然后当用户点击复选框时,告诉我的 newNSMutableDictionary 该值已被选择。但我不认为它是这样工作的,因为它是键值对。我想知道是否有其他选择,或者是否有人有保存此信息的好方法?谢谢。
I have 3 NSMutableDictionaries that hold data for 3 separate 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. 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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用所选
NSIndexPath
对象的一个NSMutableArray
来代替这样做。这样,您将拥有相当轻量的内存占用(延迟加载),并且如果您需要获取实际单元格的值,您可以使用
-tableView:cellForRowAtIndexPath:
UITableView 询问或者,更好的是,拥有一个
NSMutableDictionary
,其中一个键是封装在NSNumber
中的特定 tableview 的tag
,其值为所选索引路径的 NSMutableArray 。检索选定的索引路径就像这样简单:
Instead of doing this, you can try having one
NSMutableArray
of the selectedNSIndexPath
objects.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:
Or, even better, have one
NSMutableDictionary
with one the keys being thetag
of the specific tableview wrapped in anNSNumber
and the value being anNSMutableArray
of selected index paths.To retrieve the selected index paths would be as simple as this:
为什么不将所有可能的选择存储在 NSDictionary 中,用用户可以做出的所有选择来初始化它。当用户做出选择时,将该项目添加到“selectedItems”NSMutableDictionary 中。这样你的 NSMutableDictionary (包含选择)将只包含选定的项目,然后你可以添加和删除这个字典,而不需要改变包含用户可以做出的选择的非可变 NSDictionary ..?
why not store all possible selections in a NSDictionary, initialise it with the all the selections a user can make. When the user makes a selection add that item to a "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..?
我不确定我是否正确理解了你的问题。
但是,如果您的数据具有树的“形状”,您可能会考虑以这种方式保存信息。
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.