Note that there are some explanatory texts on larger screens.

plurals
  1. POXcode 5 iPhone property list not updating
    text
    copied!<p>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.</p> <p>*<em>CODE EDITED SO NO LONGER READING AND WRITING LIST FROM MAIN BUNDLE</em></p> <pre><code>#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 </code></pre> <p>Sorry for the massive chunk of code but thought any of it could potentially be problematic.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload