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

キーは、どの辞書でも一意で一定であると想定されています。dickey の as 値を保持しますbutton。を使用して値を変更できますsetValue:forKey

于 2012-07-20T04:55:55.510 に答える
0

vignesh が言ったように、キーは一意であると想定されているため、変更できませんが、その内容は変更できます。

 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 に答える