不确定为什么NSUserdefaults没有保存结果?

时间:2014-04-22 00:32:56

标签: ios objective-c uitableview

我正在尝试保存已检查并将未经检查的tablecells删除到NSUserdefaults但它似乎忽略它,因为for循环中的NSLog永远不会被调用,不知道为什么,这段代码有什么问题吗? (一切都显示正确,细胞得到检查和未经正确检查)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //setting up nsUser
    userDefaults = [NSUserDefaults standardUserDefaults];

    //current row text
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellName = cell.textLabel.text;

    // Check if current row is selected
    Boolean isNowChecked = NO;
    if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
    {
        isNowChecked = YES;
    }

    if(isNowChecked)
    {
        NSLog(@"UNCHECKING");
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;

        //takes away highlight from selection
        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        //retrieve from nsdefaults
        NSMutableArray *arrayOfCategories = [userDefaults objectForKey:@"categories"];

        NSMutableArray *categoriesSelected;

        //remove from nsuserdefaults
        //add to nsuserdefaults
        for (NSString* o in arrayOfCategories)
        {
            NSLog(@"%@",o);
            if([o isEqualToString:cellName]) {

            } else {
                [categoriesSelected addObject:o];
            }

        }
        //set for nsdefaults
        [userDefaults setObject:categoriesSelected forKey:@"categories"];
        [userDefaults synchronize];


            }
    else
    {
        NSLog(@"CHECKING");
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

        //takes away highlight from selection
        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        NSMutableArray *categoriesSelected;

        //add to array
        [categoriesSelected addObject:cellName];

        //retrieve from nsdefaults
        NSMutableArray *arrayOfCategories = [userDefaults objectForKey:@"categories"];

        //add to nsuserdefaults
        for (NSString* o in arrayOfCategories)
        {
            NSLog(@"%@",o);
            [categoriesSelected addObject:o];

        }
        //set for nsdefaults
        [userDefaults setObject:categoriesSelected forKey:@"categories"];
        [userDefaults synchronize];


    }
}

1 个答案:

答案 0 :(得分:4)

更改此行,否则您有一个未初始化的数组:

NSMutableArray *categoriesSelected;

为:

NSMutableArray *categoriesSelected = [[NSMutableArray alloc] init];

更新:查看代码后,实际上有很多事情需要改进。

  1. 您应该在viewDidLoad中加载一次检查类别列表,并将列表保存在实例变量中。
  2. 通过查看单元的附件类型来检查当前是否检查过单元格并不是一个好主意。您的数据模型应该告诉您检查了哪些行。
  3. 您有很多重复的代码来切换单元格的已检查状态。
  4. 使用集合而不是数组来跟踪已检查的单元格。
  5. 尝试以下更改:

    向您的班级添加NSMutableSet个实例变量:

    NSMutableSet *_checkedCategories;
    

    viewDidLoad中,加载集:

    NSArray *checkedCategories = [[NSUserDefault standardUserDefaults] objectForKey:@"categories"];
    if (checkedCategories) {
        _checkedCategories = [[NSMutableSet alloc] initWithArray:checkedCategories];
    } else {
        _checkedCategories = [[NSMutableSet alloc] init];
    }
    

    现在更新您的didSelectRow方法:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellName = ...; // get the cell name from your data model, not the cell
    
        BOOL isChecked = [_checkedCategories containsObject:cellName];
    
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (isChecked) {
            [_checkedCategories removeObject:cellName]; // no longer checked
            cell.accessoryType = UITableViewCellAccessoryNone;
        } else {
            [_checkedCategories addObject:cellName]; // now checked
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        [[NSUserDefaults standardUserDefaults] setObject:[_checkedCategories allObjects] forKey:@"categories"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }