从UITableView UISwitch写入每个项目的Plist键值

时间:2015-01-10 08:04:20

标签: ios objective-c iphone uitableview plist

我有UITableView,通过可编辑的plist从NSMutableDictionay填充,不在主捆绑中。我可以读/写/添加到plist。

UITableViewUISwitches。 plist是root / dictionary,有6个根键,每个包含4个项目,

我的钥匙是(例子)

theball     Array    (4 items)
  item 0    string   stringxssd
  item 1    string   stringgad
  item 2    string   stringgad
  item 3    string   stringrad
theswitch   Array    (4 items)
  item 0    Boolean  NO
  item 1    Boolean  YES
  item 2    Boolean  NO
  item 3    Boolean  NO

当用户打开/关闭开关时是/否。如何写入plist并仅更改theswitch键的/ let说第2项,将布尔值更改为YES?到目前为止,我不能特别改变键项的布尔值,只是键本身?我想改变每个开关更换的每个项目......

3 个答案:

答案 0 :(得分:0)

试试这个

[myDictionary setObject: [NSNumber numberWithBool: switch.isOn ] forKey:@"someKey"];

并像这样解压缩

BOOL someBool = [[myDictionary objectForKey: @"someKey"] boolValue ];

所以尝试像这样的开关动作..

-(IBAction) switchAction:(id)sender{

NSInteger swNum = [sender tag];
UISwitch swtch = (UISwitch *)sender;
BOOL theSetting = swtch.isOn;

//assuming a NSMutableDictionary property which was loaded from plist file at launch
//let us call this dictionary self.model  ..

NSArray *theBooleans = [self.model objectForKey: @"theswitch" ];
NSMutableArray *boolsMutable = [NSMutableArray arrayWithArray: theBooleans ];
[boolsMutable replaceObjectAtIndex:swNum withObject: [NSNumber numberWithBool:theSetting]];

[self.model setObject:boolsMutable forKey: @"theswitch" ];
}

答案 1 :(得分:0)

对于那些感兴趣的人。感谢Jef,我将他的代码用于我已经拥有的ibaction调用....,

-(IBAction) switchAction:(id)sender{
    NSLog(@"*MVC* switchAction ");
    NSInteger swNum = [sender tag];
    Switches = (UISwitch *)sender;
    BOOL theSetting = Switches.isOn;

    NSArray *theBooleans = [self.TheDict objectForKey: @"UserSwitch" ];
    NSMutableArray *boolsMutable = [NSMutableArray arrayWithArray: theBooleans ];
    [boolsMutable replaceObjectAtIndex:swNum withObject: [NSNumber numberWithBool:theSetting]];

    [self.TheDict setObject:boolsMutable forKey: @"UserSwitch" ];
    [TheDict writeToFile:PlistLocation atomically:YES];
    NSLog(@"*MVC* switchAction COMPLETE");

}

在我的cellForRowAtIndexPath中,在构建表时生成开关,并且交换机从plist中生成一个标记。

NSNumber *tagNumber = [SwitchTag objectAtIndex:indexPath.row];
    NSInteger blabla = [tagNumber integerValue];
// add the switchs
    Switches = nil;
    Switches = [[UISwitch alloc] initWithFrame:CGRectZero];
    Switches.tag = blabla;
    NSLog(@"*MVC* Switches.tag: %d", blabla);
    cell.accessoryView = Switches;
    BOOL switchState;
    switchState = [[theSwitch objectAtIndex:indexPath.row] boolValue];
    if (switchState == TRUE)
    {
        [Switches setOn:YES animated:YES];
    }
    else
    {
        [Switches setOn:NO animated:YES];
    }
    [Switches addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
杰夫,你的男人。感谢您的线索,代码和快速回答。

答案 2 :(得分:-1)

你不能将布尔值保存到plist它需要是一个整数或一个字符串我直接从IBOutlet UISwitch变量使用这个代码:

     [plistDict setValue:[NSString stringWithFormat:@"%d",UI_swictch1.on] forKey:@"UI_switch"];

这会更新NSMutable Dict - 当然你需要将plist写回文件,我假设你有代码。我假设你知道怎么做其余的事。