Can NSDictionary be used with TableView on iPhone?

In the UITableViewController subclass, there are some methods that you must implement to load data and handle a row select event:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; //there is only one section needed for my table view } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [myList count]; //myList is a NSDictionary already populated in viewDidLoad method } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease ]; } // indexPath.row returns an integer index, // but myList uses keys that are not integer, // I don't know how I can retrieve the value and assign it to the cell.textLabel.text return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Handle row on select event, // but indexPath.row only returns the index, // not a key of the myList NSDictionary, // this prevents me from knowing which row is selected } 

How should NSDictionary work with TableView?

What is the easiest way to do this?

+6
objective-c iphone uitableview nsdictionary
source share
2 answers

I donโ€™t understand why you want to use a dictionary (which is inherited unordered) for a task that requires answers to ordered questions (lines), but I believe that you have a dictionary from somewhere and canโ€™t change it. If so, you need to determine the order in which you want to display the keys, thereby invoking the array implicitly. One way to do this is to order another one in alphabetical order:

 // a) get an array of all the keys in your dictionary NSArray* allKeys = [myList allKeys]; // b) optionally sort them with a sort descrriptor (not shown) // c) get to the value at the row index id value = [myList objectForKey:[allKeys objectAtIndex:indexPath.row]]; 

the value is now the object selected in the case of tableView: didSelectRowAtIndexPath: or the object that is needed to process your cell in tableView: cellForRowAtIndexPath:

If the underlying NSDictionary changes, you need to reload ( [myTable reload] or the like) UITableView.

+23
source share

Yes. Here's how we did it:

In our XML syntax, we have this method that loads xml into a dictionary called dict:

 -(NSDictionary *)getNodeDictionary:(Node *)node { if (node->level == 0) return xmlData; else { NSDictionary *dict = xmlData; for(int i=0;i<node->level;i++) { if ([[dict allKeys] containsObject:SUBNODE_KEY]) dict = [[dict objectForKey:SUBNODE_KEY] objectAtIndex:*(node->branches+i)]; } return dict; } } 

And this method

 -(NSDictionary *)getDataForNode:(Node *)node { NSDictionary* dict = [[self getNodeDictionary:node] copy]; return dict; 

}

In the RadioData class, we have an instance variable:

 Node *rootNode; 

and a set of methods

 -(Node *)getSubNodesForNode:(Node *)node; -(Node *)getSubNodeForNode:(Node *)node atBranch:(NSInteger)branch; -(Node *)getParentNodeForNode:(Node *)node; -(NSInteger)getSubNodeCountForNode:(Node *)node; -(NSDictionary *)getDataForNode:(Node *)node; 

and property

 @property (nonatomic) Node *rootNode; 

Finally, in ViewController, when we run the frame, we use:

 radioData = data; curNode = data.rootNode; 

and inside cellForRowAtIndexPath we have:

 Node* sub = [radioData getSubNodeForNode:curNode atBranch:indexPath.row]; NSDictionary* dets = [radioData getDataForNode:sub]; 

and in the didSelectRowAtIndexPath file:

  Node* node = [radioData getSubNodeForNode:curNode atBranch:indexPath.row]; NSDictionary* data = [radioData getDataForNode:node]; 

This is probably more than you wanted, but this is a general outline.

+3
source share

All Articles