2011-08-30 5 views
0

J'ai un tableau mis en place comme:Accéder aux objets dans un tableau?

NSMutableArray *array = [[NSArray alloc] initWithObjects: @"Item 1", @"Item 2", @"Item 3", @"Item 4", @"Item 5", nil]; 

Et je voudrais accéder au contenu du tableau pour les afficher dans une NSString mais je ne suis pas sûr ce qu'il faut utiliser, par exemple:

[NSString stringWithFormat:@"%c", [objectAtIndex:0]]; 
[NSString stringWithFormat:@"%c", [objectAtIndex:1]]; 

Mais mon tableau est déclaré en - (void)viewDidLoad et je dois y accéder en - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - Comment y accéder et les afficher dans un NSString?

+0

Vous devez placer 'array' une instance var. Voir http://stackoverflow.com/questions/6851963/question-about-instantiating-object/6852037#6852037 – David

Répondre

1

Définissez le tableau comme une variable d'instance. Ensuite, vous pouvez y accéder à partir de n'importe quelle méthode d'instance.

+0

Et quelle serait la deuxième partie? Would [NSString stringWithFormat: "% @", [objectAtIndex: 1]; être correct pour accéder à chaque objet? –

+1

Désolé, j'ai oublié de répondre à la deuxième partie: Utilisez le format% @ avec [array objectAtIndex: idx]. – chmeee

+0

Merci beaucoup - tout est fait! –

Questions connexes