Search code examples
xcodeuitableviewbooleanplistuiswitch

Not Sure How to Update Bool Values in Plist from UISwitch in UITableViewCell


I have a table with four rows, each having a title and switch as an accessory. I would like for the plist Boolean value to be updated when the corresponding switch is tapped.

Plist:

<array>
    <dict>
        <key>Bools</key>
        <array>
            <false/>
            <false/>
            <false/>
            <false/>
        </array>
        <key>Strings</key>
        <array>
            <string>String0</string>
            <string>String1</string>
            <string>String2</string>
            <string>String3</string>
        </array>
    </dict>
</array>

Here are the data source methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
return [allergens count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {
return [[[allergens objectAtIndex: section] objectForKey: @"Strings"] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; {
return @"Header";
}

Here is my switch, with selector: switchToggled

UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];

Below is the IBAction method. I can successfully get the switch tapped to appear in the log, so I know everything works. I am just not sure how to get the correct Boolean value to be updated.

- (IBAction)switchToggled:(id)sender {
NSLog(@"switch tapped");
}

Any help is appreciated! Thanks.


Solution

  • I was so new to programming when I posted this I was not aware that I could not programmatically edit a plist in the main bundle at runtime. I now understand that to achieve the effect I want I should save the starting plist to the app documents directory and work from that. I used this article to help format the process of saving and loading data to and from the documents directory.