2013-01-20 7 views
1

이 데이터를 내 UITableView 안에 넣어야하지만 제대로 구현하는 방법에 대해 혼란스러워하고 있습니다. Boys 데이터를 Girls 데이터와 분리하기 위해 값을 올바르게 구분할 수 없습니다.iPhone SDK : UITableView에서 섹션을 만드는 방법

{ 
"QUERY": { 
    "COLUMNS": [ 
     "NAME", 
     "GENDER" 
    ], 
    "DATA": [ 
    [ 
     "Anne", 
     "Girl" 
    ], 
    [ 
     "Alex", 
     "Boy" 
    ], 
    [ 
     "Vince", 
     "Boy" 
    ], 
    [ 
     "Jack", 
     "Boy" 
    ], 
    [ 
     "Shiela", 
     "Girl" 
    ], 
    [ 
     "Stacy", 
     "Girl" 
    ] 
    ] 
}, 
"TOTALROWCOUNT": 6 
} 

나는이 코드를 가지고 :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [genderArray count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [genderArray objectAtIndex:section]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [namesArray count]; 
} 

namesArray이 genderArray 성별의 모든 값을 가지고있는 동안 NAME에 의해 반환 된 모든 값이 있습니다. 너희들이 날 도와 줄 수있어? 나는 혼란스러워진다. 어떤 도움을 주시면 감사하겠습니다. 감사!

답변

6

혼란 스러울수록 데이터를 조각으로 나눕니다. 섹션 당 하나씩 두 개의 배열이 필요합니다. 그래서 당신은 소년 이름의 한 배열과 소녀 이름의 또 다른 배열을 원합니다.

포함 된 DATA 배열을 반복하여 얻을 수 있습니다.

데이터를 NSDictionary 개체로 변환합니다. 귀하의 데이터는

NSArray* dataArray = [myDict objectForKey:@"DATA"]; 

으로 반복 ...

NSMutableArray* boys = [[NSMutableArray alloc] init]; 
    NSMutableArray* girls = [[NSMutableArray alloc] init]; 
    for (id person in dataArray) { 
     if ([[person objectAtIndex:1] isEqualToString:@"Girl"]) 
       [girls addObject:[person objectAtIndex:0]]; 
     else [boys addObject:[person objectAtIndex:0]]; 
    } 

지금 당신이 두 개의 배열, 각 하나가 ...
NSDictionary* myDict = [NSJSONSerialization JSONObjectWithData:myJsonData 
                  options:0 error:&error]; 

데이터를 추출 ... 그래서 JSON처럼 보이는 귀하의 테이블 섹션. 섹션의 배열을 확인하고 그것으로이 배열을 넣어 :

NSArray* headers = [NSArray arrayWithObjects:@"Boys",@"Girls",nil]; 

지금 데이터 소스 방법은 다음과 같이 :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     return [sections count]; 
    } 

    - (NSString *)tableView:(UITableView *)tableView 
    titleForHeaderInSection:(NSInteger)section 
    { 
     return [headers objectAtIndex:section]; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section 
    { 
     return [[sections objectAtIndex:section] count]; 
    } 

NSArray* sections = [NSArray arrayWithObjects:boys,girls,nil]; 

이 섹션 헤더에 대해 별도의 배열을 확인

마지막

- (UITableViewCell *)tableView:(UITableView *)tableView 
      cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    ... 

     cell.textLabel.text = (NSString*)[[self.sections objectAtIndex:indexPath.section] 
                 objectAtIndex:indexPath.row]; 
+0

감사합니다! 그것은 효과가있다! 늦은 밤 근무는 이러한 혼란을 야기했습니다. :) – jaytrixz