2010-10-05 51 views
1

我有一個plist類型的字典,其中包含數組類型的鍵的plist,每個數組都有三個字符串類型的項。在plist中訪問數組中的字符串iphone

我想訪問字符串並將它們傳遞給視圖控制器。

此行是成功的在我的cellForRowAtIndexPath

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

當我把在同一行中我didSelectRowAtIndexPath方法方法我得到一個BAD_ACCESS錯誤。

任何有關如何獲取字符串的建議將會有所幫助。

這裏是我的.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> 

而且這裏有兩種方法:

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

您正在錯誤地訪問indexPath的行變量。看看我的posst,它會告訴你如何擺脫EXC_BAD_ACCESS錯誤 – Pavan 2010-10-05 02:18:56

+0

NSUInteger row = [indexPath row]; 不應該工作它應該是NSUInteger row = indexPath.row;這是有效的,應該擺脫錯誤。我只是自己試過,我沒有得到任何錯誤 – Pavan 2010-10-05 03:07:15

回答

1

我的猜測是,你是不是保留aDictionarykeys,或者你有其他的內存問題是體現在代碼中的那一點。但是如果沒有其他代碼就很難說清楚。

+0

在我的頭文件中,我聲明瞭這樣的變量: – Nungster 2010-10-05 02:56:37

+0

@property(nonatomic,retain)NSDictionary * aDictionary; @property(nonatomic,retain)NSMutableArray * keys; 這足以保留變量嗎? – Nungster 2010-10-05 02:57:22

+0

取決於你如何構造它們:aDictionary = [NSDictionary dictionaryWithContentsOfFile:path]是不夠的。 – Aleph7 2010-10-05 04:47:48

0

改變這一行到

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

程序崩潰在哪裏,哪一行?

+0

如圖所示,程序不會崩潰,但不會從plist中拉出字符串。如果我取消註釋第二行,我打印到NSLog「來自plist的字符串」這會使其崩潰。但是在它之前的方法中,對NSLog的相同調用將正常運行,並在控制檯中顯示字符串。我現在不在我的Mac,但會嘗試你的電話,看看它是否呈現我期望實現的目標。 – Nungster 2010-10-05 14:07:42

+0

在帖子中,您聲明瞭Bad Access。在哪一行? – Jordan 2010-10-05 14:28:45

+0

我試過這一行,並且仍然像以前一樣在此行獲得EXC_BAD_ACCESS。 :( – Nungster 2010-10-05 21:39:03