簡體   English   中英

如何從NSMutableDictionary中刪除字典數組?

[英]How can i remove array of dictionary from NSMutableDictionary?

我有數組字典(在其中,還有數組和字典)。

我在UITableViewCellEditingStyleDelete中添加了UITableViewCellEditingStyleDelete,所以我想從字典中刪除特定的字典數組。

控制台數據視圖:

{
        A =     (
                   {
                    address = "Talala (gir)";
                    "main_id" = 1;
                    mobile = 8878876884;
                    name = "Amit Patel";
                   },
                   {
                    address = "Junagdh";
                    "main_id" = 5;
                    mobile = 4894679865;
                    name = "Arjun Patel";
                   }
                );
            J = (
                    {
                    address = "Taveli";
                    "main_id" = 6;
                    mobile = 87886356085878;
                    name = "Jasmin Patel";
                    },
                    {
                    address = "Gujarat";
                    "main_id" = 4;
                    mobile = 6636633368;
                    name = "Jatin ";
                   }
               );
            R =     (
                        {
                    address = "Mumbai";
                    "main_id" = 2;
                    mobile = 999686322;
                    name = "Rajan Patel";
                }
            );
            S =     (
                        {
                    address = "Rajkot";
                    "main_id" = 3;
                    mobile = 8866086549;
                    name = "Sumit Patel";
                }
            );
        }

我想刪除例如:陣列的索引是1從與鍵字典

我必須嘗試以下代碼

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {

        NSDictionary *dic = [[self.finalPDic objectForKey:[self.listOfHeader objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

        NSLog(@"%@",dic);

        [self.finalPDic removeObjectForKey:dic];

        NSLog(@"%@",self.finalPDic);

        [self.tblView reloadData];
    }
    if (editingStyle == UITableViewCellEditingStyleInsert)
    {
    }
}

但是我不能從NSMutableDictionary主文件中刪除任何記錄。

嘗試這個

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSString *key = self.listOfHeader[indexPath.section];

        NSMutableArray *users = [self.finalPDic[key] mutableCopy];

        [users removeObjectAtIndex:indexPath.row];
        if (users){
          self.finalDic[key] = users;
        }else{
        [self.finalDic removeObjectForKey:key];
        }
        [self.tblView reloadRowsAtIndexPaths:@[indexPath]
                                withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    if (editingStyle == UITableViewCellEditingStyleInsert){
    }
}

首先,確保數據結構的每個組件都是可變的(即基本字典,內部數組,不是強制性的,而是內部數組中的字典)。

然后,最終用此替換您的commitEditingStyle方法。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  {
    if (editingStyle == UITableViewCellEditingStyleDelete) {            
        [[self.finalPDic objectForKey:[self.listOfHeader objectAtIndex:indexPath.section]] removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    }
}
NSMutableArray * dictArray = [self.finalPDic objectForKey:indexPath.section];  
[dictArray removeObjectAtIndex:indexPath.row];

假設finalPDict帶有您在問題中顯示NSLog輸出的其中之一。 並且indexPath.section的評估結果可能為“ A”,而indexPath.row的評估結果可能為1。

當您使用字典的字典和作為字典數組的值時,何時以及如何以及從主字典中刪除單個(或多個)值會變得非常混亂。 訣竅是要看到每個對象都與主字典分開,並以新數組或字典的形式威脅該值。

以下是您應該執行的操作:為方便起見,我將假設您的詞典看起來像您在問題中給出的示例。

NSDictionary *maindict = //get your maindict here

NSArray *arr = [maindict valueForKey:someKey]; //in your example the values of your maindict is always an array.

//the array is filled with more dictionaries, apparantly objects that seem like contact details
//if you want to find a single item that is in this array you should loop over the array here.

for(NSDictionary *dict in arr)
{
    if(/*retrieve a value from the dict that you want to compare with a preset value before this function*/)
    {
        //remove the dict from the array here (or save it for after the loop so that you wont get the error that you try to mutate the array while running it)
    }

}

//now that the dict/record/contact is deleted from your array you should set it back at the same place in the maindict.
[maindict setValue:arr /*this arr is the arr with the removed record*/ forKey:someKey]; //someKey is the same key as you used earlier

//you are finished now! the maindict is updated with the removed value. the only thing left is updating your tableview so that the record is also removed visibly
[yourtableview reloadData]; 

按照步驟。 使用MutableArray並從字典中復制整個數組。

NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[self.finalPDic objectForKey:  [self.listOfHeader objectAtIndex:indexPath.section]]];

現在從字典中刪除密鑰的整個對象(將密鑰存儲在NSString *key = [self.listOfHeader objectAtIndex:indexPath.section]某個NSString *key = [self.listOfHeader objectAtIndex:indexPath.section]

  [self.finalPDic  removeObjectForKey:[self.listOfHeader objectAtIndex:indexPath.section]];

從tempArray中刪除您要基於indexPath.row的字典

    [tempArray removeObjectAtIndex:objectAtIndex:indexPath.row];

使用保存的鍵將tempArray添加到mainDict

 [self.finalPDic  setObject:tempArray forKey:key];//ie saved key.

希望這會有所幫助,我嘗試過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM