开发者

Alternatives to NSMutableDictionary?

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 开发者_JAVA百科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. 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 the tag of the specific tableview wrapped in an NSNumber and the value being an NSMutableArray of selected index paths.

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. 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.

  • You can put a Dictionary inside your Dictionary
  • You can put your data in CoreData and use NSTreeController
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜