我做了如下
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];
我想在tableview中移动单元格时更改密钥。 我发现钥匙的价值无法改变。
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSUInteger fromRow = [sourceIndexPath row];
NSUInteger toRow = [destinationIndexPath row];
}
在上面的方法中我应该怎么做。
答案 0 :(得分:0)
键应该是唯一且常量的。将dic
作为密钥button
的值。您可以使用setValue:forKey
更改值。
答案 1 :(得分: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
}
答案 2 :(得分: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];