Adding NSUserDefaults to an iPhone checklist
I'm pretty damn new to iPhone development and I'm trying to get a checklist going with persistent data. I'm using a table with a checklist using [code from this tutorial].1
I've been trying on my own for a week or two trying to make this work with NSUserDefaults, but as I said I'm pretty new, and although I prefer to figure these things out with the help of google, I either don't quite know how to search for what I want or I'm not smart enough yet to figure it out from what I've found. Basically, the checklist part works, cells all get the proper accessory when checked and unchecked, but I want them to stay persistent through close.
I know this is a pretty noob question, so it would help VERY MUCH if someone could post code for what I need, but any help I can get would be GREATLY appreciated.
Edit: Added Code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = @"MyCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
c开发者_如何学Cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
NSMutableDictionary *item = [dataArray bjectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"text"];
[item setObject:cell forKey:@"cell"];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
return cell;
}
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
UITableViewCell *cell = [item objectForKey:@"cell"];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}
Here is an example of using NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//Set a value
[defaults setBool:YES forKey:@"check1"];
//Save immediately
[defaults synchronize];
//Retrieve the value for check1
BOOL retrieved = [defaults boolForKey:@"check1"];
Update
If your dataArray only contains dictionaries with bools and strings in it then you can save the entire dataArray to NSUserDefaults
. The following code is untested but should be close to what you will need to persist your values.
-(id)yourInitMethod
{
dataArray = [(NSArray*)[[NSUserDefaults standardUserDefaults] objectForKey:@"dataArrayKey"] mutableCopy];
//Replace dictionaries with a mutable copy
for(int i = 0; i < [dataArray count]; i++)
{
//Your dictionary that you want to be mutable
NSMutableDictionary *aCopy = [(NSDictionary*)[dataArray objectAtIndex:i] mutableCopy];
[dataArray replaceObjectAtIndex:i withObject:aCopy];
[aCopy release];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//Save the updated data array
[defaults setObject:dataArray forKey:@"dataArrayKey"];
[defaults synchronize];
}
精彩评论