2016-06-17 39 views
0

我正在創建一個NSMutableDictionary並將多個NSDictionary存儲在NSMutableDictionary中。現在我店NSMutableDictionaryNSUserDefaults,然後用這個NSUserDefaults在其他屏幕:NSmutablearray是如何存儲在NSmutableDictionary目標c

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
NSData *userData = [userDefaults objectForKey:@"cartdata"]; 
cartDataDictionary = (NSMutableDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:userData]; 
NSLog(@"%@",cartDataDictionary); 
NSLog(@"%lu",(unsigned long)cartDataDictionary.count); 
NSEnumerator *enumerator = [cartDataDictionary keyEnumerator]; 
id key; 


while (key = [enumerator nextObject]) { 
    NSDictionary *list = [cartDataDictionary objectForKey:key]; 

    ViewMenu *View = [[ViewMenu alloc]init]; 
    View.ItemName = [list objectForKey:@"Dish_name"]; 
    View.itemPrice = [list valueForKey:@"Price"]; 
    View.itemQty = [list valueForKey:@"Qty"]; 
    View.itemImage = [list valueForKey:@"image"]; 
    View.stringId = [list valueForKey:@"id"]; 
    [items addObject:View]; 
} 

現在所有的數據在「項目」 NSMutable數組中現在這個項目數組通過創建的tableview控制器的tableview和顯示值現在想從我使用的項目刪除一些數據,

[self.items removeObjectAtIndex:button.tag]; 

,但這種說法是從項目刪除數組中的數據,但它是不可行我想從主NSMutableDictionary刪除數據並重新加載的tableview數據並顯示更新後的值。

+0

將關鍵字保存在View對象中,然後使用'dict [self.items [button.tag] .key] = nil' – Shubhank

+0

...並得到一個異常。 – Droppy

回答

0

這是因爲給出[self.items removeObjectAtIndex:button.tag];的示例代碼正試圖從items數組中刪除元素。如果你想從主數組中刪除數據,那麼存儲在cartDataDictionary屬性中的數據(我假設它被用作數據源?),你需要將它從中刪除。

[cartDataDictionary[<key>] removeObjectForKey:<other_key>]; 
[tableView reloadData]; 

您將需要由items數組中尋找它找到other_key

現在,當表視圖重新加載時,數據源已更新爲您要顯示的數據。

相關問題