Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从JSON文件向单元格添加值-提供了代码_Iphone_Objective C - Fatal编程技术网

Iphone 从JSON文件向单元格添加值-提供了代码

Iphone 从JSON文件向单元格添加值-提供了代码,iphone,objective-c,Iphone,Objective C,我有一个JSON文件,我需要从中提取值并显示在我的tableview上。一切正常 { "1": { "name": "Jemmy", "birthday": "1994-11-23" }, "2": { "name": "Sarah", "birthday": "1994-04-12" }, "3": { "name": "Deb", "birthday": "

我有一个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;
    }

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

您的
cellforrowatinexpath:
应该是这样的:

- (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中,您需要在
[Cell alloc]
行的末尾添加
autorelease

尝试此操作,并为重复使用的单元格更正
cellforrow路径:

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

您将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];
}        
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=[childforkey:@“生日”];
[personMutableArray addObject:deal];
}        

我使用的是ARC。如果我将
单元格.namelab
单元格.doblabel
添加到If条件之外(如您所示),那么当我滚动时,单元格会重叠。我如何避免这种情况?您的“重叠”是什么意思?您是否正确设置了单元格高度?您如何实现单元格的绘制?