2010-10-05 16 views
1

J'ai un plist avec racine de type dictionnaire contenant des clés de type array, et chaque tableau a trois éléments de type string.Accéder à la chaîne dans un tableau à l'intérieur d'un plist iphone

Je souhaite accéder aux chaînes et les transmettre à un contrôleur de vue.

Cette ligne est réussie dans mon cellForRowAtIndexPath

NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);

Quand je mets la même ligne dans ma méthode de didSelectRowAtIndexPath je reçois une erreur de BAD_ACCESS.

Toute suggestion quant à la façon d'obtenir les cordes serait utile.

Voici mon .plist

<dict> 
    <key>Derivative</key> 
    <array> 
     <string>Derivative 1</string> 
     <string>front.png</string> 
     <string>back.png</string> 
    </array> 
    <key>Rolle&apos;s Theorem</key> 
    <array> 
     <string>Rolle&apos;s Theorem 1</string> 
     <string>front.png</string> 
     <string>3bk.png</string> 
    </array> 
    <key>Chain Rule</key> 
    <array> 
     <string>Chain Rule</string> 
     <string>chainrule1.png</string> 
     <string>chainrule1_bk.png</string> 
    </array> 
</dict> 
</plist> 

Et voici les deux méthodes:

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

    static NSString *CellIdentifier = @"Cell"; 

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


    NSUInteger row = [indexPath row]; 

    cell.backgroundColor = [UIColor purpleColor]; 
    cell.textLabel.textColor = [UIColor blackColor]; 

    cell.textLabel.text = [self.keys objectAtIndex:row]; 

    // Key output to NSLOG accessing elements from the plist 
    NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]); 

    cell.textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:18.0]; 
    cell.textLabel.numberOfLines = 2; 
    cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCell_BG.png"]] autorelease]; 

    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSUInteger row = [indexPath row]; 
    NSLog(@"Row selected was: %i", row); 

    // Key output to NSLOG accessing elements from the plist 
    //NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]); 


/* 
    NSString *selectedCard = [[aDictionary objectForKey:(@"%@",[keys objectAtIndex:row])] objectAtIndex:0]; 
    NSLog(@"Showing Flash Card: %@", selectedCard); 
*/ 

    FlashCardViewController *flashVC = [[FlashCardViewController alloc] initWithNibName:@"FlashCardViewController" bundle:nil]; 

    id aKey = [keys objectAtIndex:row]; 

    flashVC.aSelectedCard = [[aDictionary objectForKey:aKey] objectAtIndex:0]; 

    [self.navigationController pushViewController:flashVC animated:YES]; 
    [flashVC release]; 

} 
+0

vous accédez incorrectement à la variable de ligne de l'indexPath. jetez un oeil à ma posst et il vous montrera comment se débarrasser de l'erreur EXC_BAD_ACCESS – Pavan

+0

NSUInteger row = [ligne indexPath]; cela ne devrait pas fonctionner il devrait être NSUInteger row = indexPath.row; et cela fonctionne et devrait se débarrasser de l'erreur. Je viens de l'essayer moi-même et je ne reçois aucune erreur – Pavan

Répondre

1

Je suppose que vous ne retenez pas aDictionary ou keys, ou si vous avez un autre problème de mémoire qui est se manifestant à ce moment-là dans le code. Mais c'est difficile à dire sans le reste du code.

+0

Dans mon fichier d'en-tête, je déclare les variables comme ceci: – Nungster

+0

@property (nonatomic, retain) NSDictionary * aDictionary; @property (nonatomic, retain) clés NSMutableArray *; est-ce suffisant pour conserver les variables? – Nungster

+0

Dépend de la façon dont vous les construisez: aDictionary = [NSDictionary dictionaryWithContentsOfFile: path] est _not_ assez. – Aleph7

0

modifier cette ligne à

flashVC.aSelectedCard = [[aDictionary objectForKey: [touches objectAtIndex: Rang]] objectAtIndex: 0];

Où se bloque le programme, quelle ligne?

+0

Comme affiché, le programme ne plante pas, mais ne tire pas la chaîne de la plist. Si je ne commente pas la deuxième ligne où j'imprime à NSLog "Strings from plist" Cela va planter. Cependant, dans la méthode précédente, le même appel à NSLog fonctionnera correctement et affichera les chaînes dans la console. Je ne suis pas à mon Mac pour le moment, mais je vais essayer votre appel et voir si cela rend ce que je cherche à réaliser. – Nungster

+0

Dans la publication, vous indiquez Mauvais accès. Sur quelle ligne? – Jordan

+0

J'ai essayé cette ligne et j'obtiens toujours l'EXC_BAD_ACCESS sur cette ligne comme avant. :( – Nungster