【问题标题】:How can i remove array of dictionary from NSMutableDictionary?如何从 NSMutableDictionary 中删除字典数组?
【发布时间】:2013-04-11 05:04:24
【问题描述】:

我有数组字典(里面还有数组和字典)。

我在 UITableView 中添加了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 来自字典的键 A

我必须尝试使用​​以下代码

- (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 中删除任何记录。

【问题讨论】:

  • 用 TableView 解释数据的映射。
  • 嗨,你可以得到像 NSArray *arr=[[dict allKeys]objectAtIndex:indexpath.row]; 这样的数组
  • a,j,r and s 是dictionary 在array 内吗?而这些dictionary 又包含array?
  • 问题的缩进不是很好。它是一个字典,每个值都有一个字典数组。
  • 你检查答案了吗。

标签: iphone ios objective-c xcode cocoa-touch


【解决方案1】:

试试这个

- (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){
    }
}

【讨论】:

    【解决方案2】:

    首先确保数据结构的每个组件都是可变的(即基本字典、内部数组,不是强制性的,但数组内部的字典也是如此)。

    然后最后用这个替换你的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];
        }
    }
    

    【讨论】:

      【解决方案3】:
      NSMutableArray * dictArray = [self.finalPDic objectForKey:indexPath.section];  
      [dictArray removeObjectAtIndex:indexPath.row];
      

      这假设 finalPDict 带有您在问题中显示 NSLog 输出的那个。 indexPath.section 可能评估为“A”,indexPath.row 可能评估为 1。

      【讨论】:

        【解决方案4】:

        当您使用字典的字典和字典数组的值时,何时以及如何从主字典中删除单个(或多个)值可能会变得非常混乱。诀窍是将每个对象都从主字典中分离出来,并将该值作为一个新的数组或字典来威胁。

        以下是您应该做的: 为方便起见,我假设您的字典看起来像您在问题中给出的示例。

        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]; 
        

        【讨论】:

          【解决方案5】:

          按照步骤操作。 取一个 MutableArray 并从字典中复制整个数组。

          NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[self.finalPDic objectForKey:  [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.
          

          希望这有帮助,我试过了。

          【讨论】:

            猜你喜欢
            • 2016-03-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-01
            相关资源
            最近更新 更多