user3105611
user3105611

Reputation: 1

Xcode 5 iPhone property list not updating

New to coding and trying to create a simple check list (like a shopping list) for part of my iOS programme. Selecting a cell changes the accessory icon ok and changing the BOOL value in the dictionary manually before running the simulator also changes the acc' icon fine. So the problem seems to be with the code for altering the BOOL value in the plist after a cell is selected. Any help would massively appreciated. As I said pretty new to it so apologies for any shoddy code or obvious mistakes.

*CODE EDITED SO NO LONGER READING AND WRITING LIST FROM MAIN BUNDLE

#import "CheckListViewController.h"
#import "ListItem.h"

@interface CheckListViewController ()

@end

@implementation CheckListViewController


{
    NSMutableArray *eventList;
}


@synthesize tableView = _tableView;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES) objectAtIndex:0];
    NSString* dataPath = [documentsDirectory stringByAppendingPathComponent:@"CheckList.plist"];

    if ( ![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
        NSString* resourceaPath = [[NSBundle mainBundle] pathForResource:@"CheckList"   ofType:@"plist"];
        [[NSFileManager defaultManager] copyItemAtPath:resourceaPath toPath:dataPath error:NULL];
    }

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"CheckList.plist"];
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    ListItem *listItem1 = [ListItem new];
    listItem1.itemName = @"Read Guide";
    listItem1.itemSelected = [dict valueForKey:@"Read Guide"];

.....
    eventList = [NSMutableArray arrayWithObjects:listItem1, listItem2, listItem3, listItem4, listItem5, listItem6, listItem7, listItem8, nil];

    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    //Return the number of rows in the section.
    return eventList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    ListItem *listItem  = [eventList objectAtIndex:indexPath.row];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

        if (listItem.itemSelected == [NSNumber numberWithBool:YES]) {
            (cell.accessoryType = UITableViewCellAccessoryCheckmark);
        } else {
            (cell.accessoryType = UITableViewCellAccessoryNone);
         }
    }

    cell.textLabel.text = listItem.itemName;

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];

    ListItem *listItem  = [eventList objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"CheckList.plist"];
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        // Reflect selection in data model
        [dict setObject:[NSNumber numberWithBool:YES] forKey:listItem.itemSelected];
        [dict writeToFile:path atomically:YES];


    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;

        // Reflect deselection in data model
        [dict setObject:[NSNumber numberWithBool:NO] forKey:listItem.itemSelected];
        [dict writeToFile:path atomically:YES];
    }

}

@end

Sorry for the massive chunk of code but thought any of it could potentially be problematic.

Upvotes: 0

Views: 429

Answers (1)

gdavis
gdavis

Reputation: 2585

It looks like the path you are saving to and load from are different. You are loading from a bundle path, and then saving to a relative path with writeToFile:atomically. If my guess is correct, the default path for that method is not back into the bundle, but the documents directory of the app. On iOS, you cannot write back to the main bundle, so there a very good chance the file is not being saved where you think it is.

Upvotes: 1

Related Questions