2014-09-02 23 views
0

我是iOS新手。我必須解析下面的JSON並將其顯示給UITableViewCell。當我解析並追加國家項目時,單個數組出現在單元格中。但所有陣列的前。等級,國家,人口,國旗不出現在細胞中。解析UITableviewcell中的JSON

如何添加數組中的所有等級,國家,人口,國旗並將其全部放入單元格中。我把他們全部放入字符串,然後進入數組。和整個數組我附加到主數組。

以下是JSON -

http://www.androidbegin.com/tutorial/jsonparsetutorial.txt 

代碼

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    {    
     NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];     
     NSArray *arrayWorldPopulation = [allDataDictionary objectForKey:@"worldpopulation"]; 
     for (NSDictionary *diction in arrayWorldPopulation) 
     { 
      NSString *country = [diction objectForKey:@"country"]; 
      NSString *population = [diction objectForKey:@"population"]; 
      NSString *flag = [diction objectForKey:@"flag"]; 

      NSArray *temparray = [[NSArray alloc] initWithObjects:rank,country,population,flag, nil]; 
      [array addObject:temparray]; 
     } 
     [maintableView reloadData]; 
    } 
+0

什麼是'tableView:cellForRowAtIndexPath:'的代碼,以及您的tableView的其他幾個數據源? – Larme 2014-09-02 10:25:37

+1

基本上,你需要學習如何編碼cellForRowAtIndexPath。這是不平凡的,沒有人會爲你做你的功課。擊中書! – 2014-09-02 12:10:28

回答

0

至少,你需要實現的tableView:numberOfRowsInSection:和的tableView:在的cellForRowAtIndexPath您的視圖控制器。這會告訴表格有多少行以及表中的每行是什麼樣的。下面的簡單代碼假設你有一個字符串數組,並且每個單元格只顯示一個字符串,並且會讓你開始。您的具體情況聽起來像可能需要定製單元設計。 This tutorial describes how to do this in a storyboard with a custom cell class

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
{ 
    return [array count]; //tell the UITableView how many are items are in the array 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    //stuff to make sure iOS is reusing cells rather than creating new ones 
    static NSString *MyIdentifier = @"MyReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
    } 

    NSString *itemFromArray = [array objectAtIndex:indexPath.row]; //get the item for that cell 

    cell.textLabel.text = itemFromArray; set the cell to display the text 
    return cell; 
}