将 NSUserDefaults 添加到 iPhone 清单

发布于 2024-10-26 04:38:56 字数 3197 浏览 0 评论 0原文

我对 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];
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2024-11-02 04:38:56

这是使用 的示例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 仅包含带有布尔值和字符串的字典,那么您可以将整个 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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文