2012-01-31 29 views
0

我有一個JSON文件,我需要從中提取值並顯示在我的tableview中。一切正常。從JSON文件向Cell添加值 - 提供的代碼

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

回答

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代碼,但在弧前,您需要添加autorelease[Cell alloc]行的末尾。

+0

我正在使用ARC。如果我在if條件之外添加'cell.namelabel'和'cell.doblabel'(如你所示),那麼當我滾動時單元格會重疊。我怎樣才能避免這種情況? – shajem 2012-02-01 01:36:57

+0

你是什麼意思「重疊?」你正確設置你的細胞高度嗎?你如何實現Cell的繪圖? – 2012-02-01 14:18:13

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