2010-12-14 35 views
0

J'ai regardé partout pour cela, mais je suis incapable de trouver la réponse exacte ... tous ont de légères variations.NSArray imbriqué dans UITableView

Quoi qu'il en soit, j'appelle une page JSON qui renvoie les éléments suivants (du NSLog):

{ 
    messages =   { 
     1 =    { 
      Body = "This is the body of message 1"; 
      Title = "Message 1"; 
     }; 
     2 =    { 
      Body = "This is the body of message 2"; 
      Title = "Message 2"; 
     }; 
    }; 
} 

Je puis enregistrer les données dans un NSDictionary (appelé messageArray). (Le tableau est un NSMutableArray)

alors je fais:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    //put rowsarray into dictionary 
    NSDictionary *dictionary = [messageArray objectAtIndex:indexPath.section]; 

    //new dictionary into array 
    NSArray *messages = [dictionary objectForKey:@"messages"]; 

    NSLog(@"the message array = %@",messages); 

    //this fails 
    cell.textLabel.text = [messages objectAtIndex:indexPath.row]; 

return cell; 

le NSLog (donc je suppose mon tableau JSON fonctionne correctement) retourné:

the message array = { 
1 =  { 
    Body = "This is the body of message 1"; 
    Title = "Message 1"; 
}; 
2 =  { 
    Body = "This is the body of message 2"; 
    Title = "Message 2"; 
}; 

}

Je comprends que je n'étiquette pas le textlabels.text correctement, mais je ne sais pas comment faire pour boucler à travers le tableau "messages", pour afficher toutes les valeurs "Title" du tableau, pour afficher sur mon interface utilisateur Liste TableView. Je suis certain qu'il me manque quelque chose de si simple ... mais il m'a échappé jusqu'à présent. Tous les liens bienvenue ... je vais continuer à me chercher ....

Répondre

1
NSArray *messages = [dictionary objectForKey:@"messages"]; 

quel est le besoin de cette ligne si vous obtenez votre dictionnaire ici

NSDictionary *dictionary = [messageArray objectAtIndex:indexPath.section]; 

maintenant, vous savez quelles sont les clés pour attacher des données au dictionnaire.

alors simplement

cell.textLabel.text = [dictionary valueForKey:@"Title"]; 
cell.detailTextLabel.text= [dictionary valueForKey:@"Body"]; 
0

Cet appel

ressemble à elle retourne un dictionnaire et que vous essayez de bourrer dans un champ de test. Je confirme que c'est un dictionnaire d'abord, quelque chose comme:

NSLog(@"%@", [messages objectAtIndex:indexPath.row]) 

Si ce n'est pas une chaîne, qui peut être votre problème.

Pour obtenir la chaîne que vous voulez, vous pouvez faire quelque chose comme: la question

triés
[[messages objectAtIndex:indexPath.row] valueForKey:@"Title"] 
+0

i ont ajouté cette ligne: NSLog (@ "% @", [messages objectAtIndex: indexPath.row]) ;, et il n'y a pas d'amour. je reçois le sélecteur non reconnu envoyé à l'instance. – btwong

+0

try logging [messages class] – MCannon

0
cell.textLabel.text = [[messages objectAtIndex:indexPath.row] objectForKey:@"Title"]; 
cell.detailTextLabel.detailLabel = [[messages objectAtIndex:indexPath.row] objectForKey:@"Body"]; 
+0

J'ai essayé à la fois les idées de définir le textLabel, et je ne reçois rien. Je pense que je dois regarder mon dictionnaire et/ou obtenir une chaîne. – btwong

+0

qu'est-ce que vous utilisez pour convertir l'objet json à un dictionnaire? – griotspeak

0

j'ai "semi". J'ai supprimé les index des données JSON il ressemble maintenant:

messages =  (
      { 
     Body = "This is the body of message 1"; 
     Title = "Message 1"; 
    }, 
      { 
     Body = "This is the body of message 2"; 
     Title = "Message 2"; 
    } 
); 

et ont utilisé cela pour le peuplement de la table:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    //put rowsarray into dictionary 
    NSDictionary *dictionary = [messageArray objectAtIndex:indexPath.section]; 

    NSLog(@"dictionary = %@", dictionary); 

    //new dictionary into array 
    NSArray *messages = [dictionary objectForKey:@"messages"]; 

    //NSArray *message=[[messages allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
    NSLog(@"array messages = %@", messages); 

    cell.textLabel.text = [[messages objectAtIndex:indexPath.row] objectForKey:@"Title"]; 

    return cell; 
} 

J'espère que cela aide quelqu'un.