2014-09-02 8 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

+1

기본적으로 cellForRowAtIndexPath를 코딩하는 방법을 배워야합니다. 그것은 사소한 일이 아니며 아무도 당신을 위해 숙제를하지 않을 것입니다. 책을 쳐! –

답변

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