0

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

UITableViewCellEditingStyleDelete在我的 UITableView 中添加了,所以我想从字典中删除特定的字典数组。

数据的控制台视图:

{
        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";
                }
            );
        }

我想删除例如:数组的索引来自带有键A1的字典

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

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

但我无法从 main 中删除任何记录NSMutableDictionary

4

5 回答 5

2

尝试这个

- (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){
    }
}
于 2013-04-18T07:46:25.423 回答
1

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

然后最后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];
    }
}
于 2013-04-19T06:34:00.640 回答
0
NSMutableArray * dictArray = [self.finalPDic objectForKey:indexPath.section];  
[dictArray removeObjectAtIndex:indexPath.row];

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

于 2013-04-18T07:38:44.327 回答
0

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

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

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]; 
于 2013-04-18T07:43:20.950 回答
0

按照步骤。取一个 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.

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

于 2013-04-18T08:14:43.897 回答