从JSON文件向Cell添加值 - 提供的代码

时间:2012-01-31 18:32:44

标签: iphone objective-c

我有一个JSON文件,我需要从中提取值并显示在我的tableview上。一切正常。

{
    "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"
    }
} 

当我显示值时,它不会按照记录中给出的1,2,3,4的顺序显示。它只是随机显示。我在下面包含了我的代码,我需要对其进行修改,以便按上面给出的顺序显示内容。有人可以帮忙吗?

- (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;
    }

3 个答案:

答案 0 :(得分:1)

你没有正确地重复使用你的手机。如果正在重用该单元,则无法对其进行配置。

您的cellForRowAtIndexPath:应该是这样的:

- (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;
}

请注意,这是正确的ARC代码,但在ARC之前,您需要将autorelease添加到[Cell alloc]行的末尾。

答案 1 :(得分:0)

尝试此操作,并更正cellForRowAtIndexPath:重复使用的单元格

- (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];    
    }

答案 2 :(得分:0)

将JSON中的数据存储在不存储有序数据的字典中。您必须按顺序在数组中获取数据或对数组进行排序。

您可以对存储字典数据的数组进行排序,如果您希望按名称的字母顺序对其进行排序,则可以执行以下操作:

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


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

如果您希望数据表示为JSON,您应该执行以下操作:

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];
}