shajem
shajem

Reputation: 2121

Adding values to Cell from JSON file - code provided

I have a JSON file, and i need to extract values from it and display on my tableview. Everything works fine.

{
    "1": {
        "name": "Jemmy",
        "birthday": "1994-11-23"
    },
    "2": {
        "name": "Sarah",
        "birthday": "1994-04-12"
    },
    "3": {
        "name": "Deb",
        "birthday": "1994-11-23"
    },
    "4": {
        "name": "Sam",
        "birthday": "1994-11-23"
    }
} 

When i display the values, it doesn't get displayed in the order of 1,2,3,4 as given in the records. It just gets displayed randomly. I have included my code below, I need it to be modified so i could display the content in the order given above. Can someone please help ?

- (void)requestSuccessfullyCompleted:(ASIHTTPRequest *)request{
                NSString *responseString = [request responseString];
                SBJsonParser *parser = [SBJsonParser new];               
                id content = [responseString JSONValue];
                if(!content){       
                    return;
                }
                NSDictionary *personDictionary = content;
                NSMutableArray *personMutableArray = [[NSMutableArray alloc] init];
                for (NSDictionary *childDictionary in personDictionary.allValues)
                {
                    Deal *deal = [[Deal alloc] init];            
                    deal.name=[childDictionary objectForKey:@"name"];
                    deal.dob=[childDictionary objectForKey:@"birthday"];
                    [personMutableArray addObject:deal];
                }        
                self.personArray = [NSArray arrayWithArray:personMutableArray];    
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {        
            Person *person = [self.personArray objectAtIndex:indexPath.row];        
            cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault 
                               reuseIdentifier:CellIdentifier];
            cell.namelabel.text=person.name;   
            cell.doblabel.text=person.dob;  
        }
        return cell;
    }

Upvotes: 0

Views: 167

Answers (3)

Alex
Alex

Reputation: 5210

You store the data from the JSON in a dictionary which doesn't store ordered data. You would have to get that data in order in an array or sort the array afterwords.

You could sort the array in which you store the dictionary data, if you wish to sort it alphabetically by name you can do this:

self.personArray = [personMutableArray sortedArrayUsingSelector:@selector(compare:)];


- (NSComparisonResult)compare:(Deal *)dealObject {
    return [self.name compare:dealObject.name];
}

If you want the data to be represented just as the JSON you should do something like this:

for (int i = 1; i < [personDictionary.allValues count]; i++)
{
    NSDictionary *childDictionary = [[personDictionary objectForKey:[NSString stringWithFormat:@"%d", i]];

    Deal *deal = [[Deal alloc] init];            
    deal.name=[childDictionary objectForKey:@"name"];
    deal.dob=[childDictionary objectForKey:@"birthday"];
    [personMutableArray addObject:deal];
}        

Upvotes: 0

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

Try this and also correct your cellForRowAtIndexPath: for reused cells

- (void)requestSuccessfullyCompleted:(ASIHTTPRequest *)request{
                NSString *responseString = [request responseString];
                SBJsonParser *parser = [SBJsonParser new];               
                id content = [responseString JSONValue];
                if(!content){       
                    return;
                }
                NSDictionary *personDictionary = content;
                NSMutableArray *personMutableArray = [[NSMutableArray alloc] init];
                NSArray *array = [personDictionary allKeys];
                NSArray * sortedArray = [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

                for (NSString *str in sortedArray)
                {
                    NSDictionary *childDictionary = [personDictionary objectForKey:str];
                    Deal *deal = [[Deal alloc] init];            
                    deal.name=[childDictionary objectForKey:@"name"];
                    deal.dob=[childDictionary objectForKey:@"birthday"];
                    [personMutableArray addObject:deal];
                }        
                self.personArray = [NSArray arrayWithArray:personMutableArray];    
    }

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299345

You're not reusing your cell correctly. If the cell is being reused, you fail to configure it.

Your cellForRowAtIndexPath: should be this way:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {        
        cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault 
                           reuseIdentifier:CellIdentifier];
    }
    Person *person = [self.personArray objectAtIndex:indexPath.row];        
    cell.namelabel.text=person.name;   
    cell.doblabel.text=person.dob;  
    return cell;
}

Note that this is correct ARC code, but in pre-ARC, you need to add autorelease to the end of the [Cell alloc] line.

Upvotes: 1

Related Questions