2012-01-05 1 views
0

NSArrayNSDictionaries이고 각 사전에는 동일한 키가 있습니다. 배열에있는 각 사전에 대한 섹션을 원합니다.NSArray를 사용하여 UITableView로드

예를 들어 배열에 4 개의 사전이있는 경우 테이블의 섹션은 4 개가됩니다. 그런 다음 각 섹션의 각 셀은 사전의 해당 값과 일치합니다.

쉬운 방법이 있나요?

예를 들어 각 사전에 3 개의 키, 이름, 성별 및 날짜가있는 3 개의 사전이있는 NSArray가 있습니다.

즉, 표에는 3 개의 섹션이 있어야하며 각 섹션 제목은 해당 사전의 날짜 키입니다. 그런 다음 이름 및 성별 값에 해당하는 섹션에 대해 2 개의 셀이 있습니다.

+0

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html 참조 : numberOfSections, numberOfRowsInSection –

+0

예, 저는 이러한 방법에 익숙하지만 'titleForHeaderInSection' 메서드에서 사전에 올바르게 날짜 키를 얻는 방법을 모르겠습니다. – Jon

답변

1

씨야 나는 그 방법에 익숙 해요,하지만 난 사전 titleForHeaderInSection 방법에 올바르게 설정에서 날짜 키를 얻는 방법을 모르겠어요.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    return (NSString *)[myArray objectAtIndex:section] objectForKey:@"date"]; 
} 

이 완벽하게 일했다. 그러나 비슷한 방법으로 cellForRowAtIndexPath의 코드를 편집하려면 어떻게해야합니까?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    /* Create myCell UITableViewCell */ 

    NSString *value = @"Unknown"; 
    if (indexPath.row == 0){ 
     value = (NSString *)[[myArray objectAtIndex:indexPath.section] objectForKey:"name"]; 
    }else if (indexPath.row == 1){ 
     value = (NSString *)[[myArray objectAtIndex:indexPath.section] objectForKey:@"gender"]; 
    } 

    myCell.textPropertyOrWhatever = value; 
    return myCell; 
} 

당신은 심지어이 모든 것을하려고합니까? : P

+0

이것은 완벽하게 작동했습니다. 그러나'cellForRowAtIndexPath'에서 비슷한 방식으로 코드를 편집하려면 어떻게해야합니까? – Jon

관련 문제