将NSUserDefaults添加到iPhone核对清单

时间:2011-03-24 19:53:32

标签: iphone uitableview nsuserdefaults datapersistance

我对iPhone开发非常陌生,而且我正在尝试使用持久数据获取清单。我正在使用一个包含清单的表格,使用[本教程中的代码]。1

我一直在努力尝试使用NSUserDefaults一两个星期,但正如我所说,我很新,虽然我更喜欢在谷歌的帮助下解决这些问题,但我或者不知道如何搜索我想要的东西,或者我还不够聪明,但还没有从我发现的东西中找到答案。基本上,清单部分工作,单元格在检查和取消选中时都会获得正确的附件,但我希望它们通过关闭保持持久性。

我知道这是一个非常棒的问题,所以如果有人可以为我需要的东西发布代码,那将会非常有用,但是我能得到的任何帮助都会非常感激。

编辑:添加代码

- (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];
        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];
}

1 个答案:

答案 0 :(得分:2)

以下是使用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"];

<强>更新

如果您的dataArray只包含带有bool和字符串的字典,那么您可以将整个dataArray保存到NSUserDefaults。以下代码未经测试,但应该接近保存您的值所需的代码。

-(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];
}