2012-02-08 78 views
0

我有以下代碼:沒有明顯的理由設置沒有的NSMutableDictionary值

//in .h file: NSMutableDictionary* expandedSections; 
//in Init method: expandedSections = [[NSMutableDictionary alloc] init]; 

- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    NSString* key = [NSString stringWithFormat:@"%d", indexPath.row]; 
    NSLog(@"Key: %@", key); 
    if ([expandedSections objectForKey:key] == nil) 
    { 
     NSLog(@"Value Should be: %@", [NSNumber numberWithBool:YES]); 
     [expandedSections setObject:[NSNumber numberWithBool:YES] forKey:key]; 
    } 
    else 
    { 
     [expandedSections setObject:[NSNumber numberWithBool:![[expandedSections objectForKey:key] boolValue]] forKey:key]; 
    } 
    NSLog(@"Value: %@", [expandedSections objectForKey:key]); 
    [tableView beginUpdates]; 
    [tableView endUpdates]; 
} 

- (float) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString* key = [NSString stringWithFormat:@"%d", indexPath.row]; 
    NSLog(@"Key: %@", key); 
    NSLog(@"Value: %@", [expandedSections objectForKey:key]); 
    if ([expandedSections objectForKey:key] == nil || ![[expandedSections objectForKey:key] boolValue]) 
    { 
     return 44; 
    } 
    else 
    { 
     return 88; 
    } 
} 

我期待這對輸出到我在我的UITableView點按行記錄的1所述的交流,爲0 。相應地,展開並摺疊我點擊的那一行。

出於某種原因,NSDictionary從來沒有任何鍵/值對,因此行不會展開。從heightForRow方法記錄出來爲:

2012-02-08 11:36:30.291 MappApp[5040:707] Key: 0 
2012-02-08 11:36:30.293 MappApp[5040:707] Value: (null) 

從didSlectRowAt法測井出來爲:

2012-02-08 11:36:44.551 MappApp[5040:707] Key: 0 
2012-02-08 11:36:46.530 MappApp[5040:707] Value Should be: 1 
2012-02-08 11:36:50.723 MappApp[5040:707] Value: (null) 

我在做什麼錯?

+0

讓它成爲一個NSMutableDictionary – Stavash 2012-02-08 11:45:59

+0

它是我的代碼中的一個NSMutableDictionary - 這是評論中的拼寫錯誤 – 2012-02-08 11:48:08

+0

你能不能嗎?區分NSLog中的didSelect ..和heightFor ..方法? – Ilanchezhian 2012-02-08 11:50:30

回答

0

如果expandedSectionsnil,則日誌記錄輸出有意義,即您從未創建或因某種原因將其重置。

您應該首先檢查您的類的初始化代碼。

+0

Thankyou - 這的確是正確的答案 - 我的init從未被稱爲... – 2012-02-09 08:38:04