Francis F
Francis F

Reputation: 3285

Getting data inside from a key from an array of dictionaries in ios

I have a dictionary defined as

@property (retain, nonatomic) NSMutableDictionary *MyDictionary;

I tried to get the output like this

NSLog(@"%@",MyDictionary);

THIS IS THE OUTPUT I GET

{ Category=(
        {
        code=12; Name="John Smith"
        },
        {
        code=21; Name="Bobby Smith"
        },
        {
        code=31; Name="Smith Jones"
        } );

Detail = {
        code=1;
        Text="Developer"
        };
}

My table cells are populated with names, John Smith, Bobby Smith and so on. What I need is to get the code when I click on a particular cell. For eg if I click on name John Smith I should get the value 12

This is my code so far

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSArray *allNames = [MyDictionary allValues];
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"Name == %@", cell.textLabel.text];
    NSArray *filteredNames = [allNames filteredArrayUsingPredicate:resultPredicate];
    NSLog(@"%@",filteredNames);

}

The filteredNames doesnt contain any filtered objects. How do I get the value of code?

Upvotes: 1

Views: 2394

Answers (3)

Matthew Hallatt
Matthew Hallatt

Reputation: 1380

I suggest creating a subclass of UITableViewCell that simply has a label and an integer value as properties. Then initialise it as below:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellClassNameHere"];
  cell.codeValue = [[myDic objectForKey:@"Category"] objectAtIndex:indexPath.row];
}

Add the following code also in order to retrieve the code when the cell is touched:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  NSString *codeValue = cell.codeValue;
  NSLog(@"%@", codeValue);
}

As mentioned in a comment above, make sure your NSDictionary is being initialised properly:

NSMutableDictionary myDic = [[NSMutableDictionary alloc] init]; //This can be replaced with however you're getting your code values.

Then you should be good to go! It should be noted that no code posted in this answer is tested, so let me know if you have any issues!

Matt

Upvotes: 0

Cy-4AH
Cy-4AH

Reputation: 4585

Assign code to cell's tag and use it in your's didSelectRowAtIndexPath

Upvotes: 0

iPatel
iPatel

Reputation: 47089

EDITED:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
   NSString *getCodeValue = [[[myDic objectForKey:@"Category"] objectAtIndex:indexPath.row] objectForKey:@"code"]
   NSLog (@"%@", getCodeValue)      
}

USe this code in didSelectRowAtIndexPath method

Upvotes: 1

Related Questions