无法保存复选标记以显示在 UITableView 上

Can't get saved checkmarks to appear on UITableView

我有一个 UITableView,用户可以在其中点击单元格,单元格附件视图上会出现一个复选标记。当单元格被点击时,索引路径被保存到一个数组,数组被保存到磁盘。

当我离开并返回到视图控制器时,我无法让复选标记重新出现在 UITableView 上。

- (void)viewWillAppear:(BOOL)animated {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:@"selectedCells"];
    self.retrievedIndexPaths = [[NSArray alloc] initWithObjects:    

    [NSKeyedUnarchiver unarchiveObjectWithData:data], nil];

    NSLog(@"%@", self.retrievedIndexPaths);

}

可能是我哪里做错了:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = self.categoriesArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];

    for (NSIndexPath *retrievedIndexPath in self.retrievedIndexPaths)
    {
        if (retrievedIndexPath == indexPath)
        {   // Never hits here
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

return cell;

}

保存复选标记数据:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.selectedRowsArray addObject:cell.textLabel.text];
    self.lastIndexPath = indexPath;
    [self.selectedIndexPathsMutableArray addObject:self.lastIndexPath];

}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
    [self.selectedRowsArray removeObject:cell.textLabel.text];
    [self.selectedIndexPathsMutableArray removeObject:indexPath];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];

NSLog(@"%@", self.selectedRowsArray);
NSLog(@"%@", self.selectedIndexPathsMutableArray);
}

#pragma mark - UIBUTTON

- (IBAction)doneButton:(UIBarButtonItem *)sender
{
    NSLog(@"button pressed");

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:self.selectedIndexPathsMutableArray] forKey:@"selectedCells"];
    [defaults synchronize];


    [[PFUser currentUser] setObject:self.selectedRowsArray forKey:@"interests"];
    [[PFUser currentUser] saveInBackground];
}

indexPath 是一个对象,因此您不应使用“==”来测试是否相等。也不需要遍历数组,只需使用 containsObject,

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = self.categoriesArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];

    if ([self.retrievedIndexPaths containsObject:indexPath]){ 
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;

}

cell.accessoryType = UITableViewCellAccessoryCheckmark