1

我做了如下

  NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:a1,@"0",a2,@"1",a3,@"2",a4,@"3", nil];
  NSDictionary *userbtn=[[NSDictionary alloc] initWithObjectsAndKeys:dic,@"button", nil];
  [[NSUserDefaults standardUserDefaults] registerDefaults:userbtn];

我想在表格视图中移动单元格时更改密钥。我发现键的值无法更改。

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSUInteger fromRow = [sourceIndexPath row];
    NSUInteger toRow = [destinationIndexPath row];

}

我该怎么办......在上面的方法中。

4

3 回答 3

0
   NSDictionary *mydic=[[NSDictionary alloc] initWithObjectsAndKeys:@"Object Zero",@"0",@"Object One",@"1",@"Object Two",@"2",@"Object Three",@"3", nil];
            [[NSUserDefaults standardUserDefaults] setObject:mydic forKey:@"MyDictionary"];
            [[NSUserDefaults standardUserDefaults] synchronize];

                // then for replacing values
            [[[[NSUserDefaults standardUserDefaults] objectForKey:@"MyDictionary"] mutableCopy] setObject:@"Object DOUBLE Zero" forKey:@"0"];
                // above code will replace string object @"Object Zero" to @"Object DOUBLE Zero" for key @"0"
            [[NSUserDefaults standardUserDefaults] synchronize];
于 2014-06-02T09:16:08.087 回答
0

正如 vignesh 所说, Key 应该是唯一的,因此不能更改,但可以更改其内容:

 NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithObjectsAndKeys:a1,@"0",a2,@"1",a3,@"2",a4,@"3", nil];
NSMutableDictionary *userbtn=[[NSDictionary alloc] initWithObjectsAndKeys:dic,@"button", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:userbtn];

现在

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
  NSUInteger fromRow = [sourceIndexPath row];
  NSString *fromstr = [[userbtn objectForKey:@"button"]objectForKey:[NSString stringWithFormat:@"%d",fromRow]]; // firstly take content at index before interchanging

  NSUInteger toRow = [destinationIndexPath row];
  NSString *tostr = [[userbtn objectForKey:@"button"]objectForKey:[NSString stringWithFormat:@"%d",toRow]]; // firstly take content at index before interchanging

 [[userbtn objectForKey:@"button"] setObject:fromstr forKey:[NSString stringWithFormat:@"%d",toRow]]; //interchange value but key is same

 [[userbtn objectForKey:@"button"] setObject:tostr forKey:[NSString stringWithFormat:@"%d",fromRow]];//interchange value but key is same

}
于 2012-07-20T05:09:32.723 回答
0

对于任何字典,键都应该是唯一且恒定的。将您的dicas 值保留为 key button。您可以使用 更改值setValue:forKey

于 2012-07-20T04:55:55.510 回答