Note that there are some explanatory texts on larger screens.

plurals
  1. POPersisting Checklists on a UITableView using NSUserDefults
    text
    copied!<p>I have a very simple table view which shows a list of days. Once the user selects which days are relevant to them this data is saved in NSUserdefaults. I then need the check marks to remain once the user has exited then re-entered the table view. </p> <p>I am very close to getting my desired functionality - I can save the array of check marked items and get it to persist using NSUserDefaults but I don't know how to make these selections persist (keep a check mark next to the selected item) once a user has exited then re-entered the table view. </p> <p>I know that I need to edit the <strong>cellForRowAtIndexPath</strong> method but I am not sure exactly what to do. Any help would be greatly appreciated.</p> <p>I have attached my code below:</p> <pre><code>#import "DayView.h" @implementation DayView @synthesize sourceArray; @synthesize selectedArray; - (id)init { self = [super initWithStyle:UITableViewStyleGrouped]; if (self) { // Custom initialization [[self navigationItem] setTitle:@"Days"]; [[self tableView] setBackgroundColor:[UIColor clearColor]]; } return self; } - (void)viewWillDisappear:(BOOL)animated { // create a standardUserDefaults variable NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults]; // Convert array to string NSString *time = [[selectedArray valueForKey:@"description"] componentsJoinedByString:@","]; // saving an NSString [standardUserDefaults setObject:time forKey:@"string"]; NSLog(@"Disapear: %@", time); } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // create a standardUserDefaults variable NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults]; // getting an NSString object NSString *myString = [standardUserDefaults stringForKey:@"string"]; NSLog(@"Appear: %@", myString); NSMutableArray * tempArray = [[NSMutableArray alloc] init]; [self setSelectedArray:tempArray]; [tempArray release]; NSArray * tempSource = [[NSArray alloc] initWithObjects:@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday",nil]; [self setSourceArray:tempSource]; [tempSource release]; [self.tableView reloadData]; } #pragma mark - #pragma mark Table view data source // Customize the number of sections in the table view. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.sourceArray count];; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } NSString *time = [sourceArray objectAtIndex:indexPath.row]; cell.textLabel.text = time; if ([self.selectedArray containsObject:[self.sourceArray objectAtIndex:indexPath.row]]) { [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; } else { [cell setAccessoryType:UITableViewCellAccessoryNone]; } NSLog(@"Selected Days: %@", selectedArray); return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"Which times you are available?"; } #pragma mark - #pragma mark Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.selectedArray containsObject:[self.sourceArray objectAtIndex:indexPath.row]]){ [self.selectedArray removeObjectAtIndex:[self.selectedArray indexOfObject: [self.sourceArray objectAtIndex:indexPath.row]]]; } else { [self.selectedArray addObject:[self.sourceArray objectAtIndex:indexPath.row]]; } [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end </code></pre>
 

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