0

My app is loading some data thought JSON and everything is working fine, but when I try to display those data in my UITableView cell, nothing happens. My code is below:

Get Data(JSON):

-(void)fetchedData:(NSData *)responseData {

    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSArray* latestLoans = [json objectForKey:@"loans"];

    testeDictionary = [latestLoans objectAtIndex:0];

    testeLabel.text = [NSString stringWithFormat:@"%@",[testeDictionary objectForKey:@"id"]];

    testeString = [testeDictionary objectForKey:@"username"];
    [miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:testeString,@"username",nil]];


}

UITableView :

-(UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    UITableViewCell *cell = (UITableViewCell *)[self.settingsTableView dequeueReusableCellWithIdentifier:@"CellD"];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellD" owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }


    if ([indexPath row] == 0) {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellA" owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];


        NSDictionary *itemAtIndex = (NSDictionary *)[miArray objectAtIndex:indexPath.row];


        UILabel *usernameString = (UILabel *)[cell viewWithTag:1];
        usernameString.text = [itemAtIndex objectForKey:@"id"]; <== MUST DISPLAY JSON VALUE


    }

    return cell;

}

More clearly I need to display this [testeDictionary objectForKey:@"id"] on the usernameString.text?

3
  • Quick debugging: a. do you have data in itemAtIndex, b. does usernameString contain anything and is it in fact a UILabel, c. does [itemAtIndex objectForKey:@"id"] return a string? Commented May 21, 2012 at 1:07
  • Just look at the code and you'll se it.The usernameString is a UILabl created programmatically and the objectForKey:@"id" one of the values i retrieve from the JSON data.About the itemAtIndex,no i don't know,everytime i try to use it get a null value! Commented May 21, 2012 at 1:13
  • Simply casting something to a UILabel does not make it a label. [cell viewWithTag:1] could be returning nil. But if you say [itemAtIndex objectForKey:@"id"] returns a null value, then your dictionary does not have a key named "id". Could it be hidden in a substructure? Have you tried NSLog to inspect the dictionary keys? Commented May 21, 2012 at 1:18

1 Answer 1

1

You are not storing the id

[miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:testeString,@"username",nil]];

I suppose what you want to be doing is something like this

NSString *idString = [NSString stringWithFormat:@"%@", [testeDictionary objectForKey:@"id"]];
[miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                          testeString, @"username",
                                          idString, @"id", 
                                          nil]];

EDIT (explanation)

In your method fetchedData: you extract the id and set the text of some label to the id.

testeLabel.text = [NSString stringWithFormat:@"%@",[testeDictionary objectForKey:@"id"]];

After that you forget about the id. You then proceed to extract the username and you create a dictionary containing only the username and add that dictionary to an array called miArray.

[miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:testeString,@"username",nil]];

Notice that you do not specify any key named "id".

Later, you fetch a dictionary from miArray. This dictionary is the one you created with only one key, i.e. "username". You tell it to get an object for the key "id", but since you never specified that key, you get a nil value.

Bottom line, try my solution.

Sign up to request clarification or add additional context in comments.

2 Comments

You did the same thing i did in my code Paul!I need to the UILabel that i've create programmatically(Look at the second part of code,the UILabel is called usernameString) to display the data i've fetched from the JSON(in the first part of the code you can see that testeLabel.text = [NSString stringWithFormat:@"%@",[testeDictionary objectForKey:@"id"]]; |This part returns me a value == > I NEED TO DISPLAY THESE VALUE ON MY usernameString.text)
I am definitely not doing the same thing as you do. Please see my edited answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.