2012-01-05 2 views
0

J'ai un NSArray de NSDictionaries où chaque dictionnaire a les mêmes clés. Je veux une section pour chaque dictionnaire du tableau. Par exemple, si le tableau a 4 dictionnaires, la table aura 4 sections. Ensuite, chaque cellule de chaque section correspondra à cette valeur dans le dictionnaire.Charger UITableView avec NSArray

Y at-il un moyen facile de faire cela? Par exemple, j'ai un NSArray avec 3 dictionnaires où chaque dictionnaire a 3 clés, le nom, le genre et la date. Cela signifie que la table doit comporter 3 sections, où chaque titre de section est la clé de date pour son dictionnaire. Ensuite, il y a 2 cellules pour cette section correspondant au nom et aux valeurs de genre.

+0

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

+0

Ya je suis familier avec ces méthodes, mais je Je ne sais pas comment obtenir la clé de date du dictionnaire correctement configuré dans la méthode 'titleForHeaderInSection'. – Jon

Répondre

1

Ya je suis avec ces méthodes familar, mais je ne suis pas sûr de savoir comment obtenir la clé du dictionnaire de configuration correctement dans la méthode de titleForHeaderInSection date .

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

Cela a fonctionné parfaitement. Mais comment puis-je modifier le code dans cellForRowAtIndexPath d'une manière similaire?

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

Vous essayez même du tout? : P

+0

Cela a fonctionné parfaitement. Mais comment puis-je éditer le code dans 'cellForRowAtIndexPath' d'une manière similaire? – Jon

Questions connexes