ios - 不确定为什么 NSUserdefaults 不保存结果?

标签 ios objective-c uitableview

我正在尝试将选中的表格单元格保存并删除未选中的表格单元格到 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];


    }
}

最佳答案

改变这一行,否则你有一个未初始化的数组:

NSMutableArray *categoriesSelected;

到:

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

更新:在查看您的代码后,实际上有很多地方需要改进。

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

尝试以下更改:

在你的类中添加一个 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];
}

关于ios - 不确定为什么 NSUserdefaults 不保存结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23208337/

相关文章:

ios - 奇怪的 UIView-Encapsulated-Layout-Height 错误

ios - 在 Swift 中以编程方式向消息应用程序添加标签?

ios - 我可以使用 Siri Shortcut 更改变量值吗?

objective-c - 在传递给构造函数的 block 中引用 self

ios - 在 iOS 库中重命名类的最佳方法是什么?

swift - 如何更改一个表格 View 单元格的分隔符颜色?

ios - 如何在 iOS 中 UITableViewCell 的右侧放置另一个 TextLabel

ios - 使用 Firestore diff 的正确方法

objective-c - 如何更改 NSOutlineView 的每个单元格的颜色或 NSTableCellView

ios - 索引路径中行的单元格仅调用一次