Ios Can';无法将保存的复选标记显示在UITableView上

Ios Can';无法将保存的复选标记显示在UITableView上,ios,objective-c,uitableview,nsuserdefaults,didselectrowatindexpath,Ios,Objective C,Uitableview,Nsuserdefaults,Didselectrowatindexpath,我有一个UITableView,用户可以点击一个单元格,单元格附件视图上会出现一个复选标记。点击单元格时,索引路径保存到阵列,阵列保存到磁盘 当我离开并返回视图控制器时,无法在UITableView上重新显示复选标记 - (void)viewWillAppear:(BOOL)animated { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSData *data = [defaults ob

我有一个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];
}

索引XPath是一个对象,所以不应该测试是否与“=”相等。也不需要在数组中循环,只需使用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

使用NSKeyedArchiver的具体原因是什么?您可以直接使用setObject谢谢,我试过了,但仍然不起作用。断点从未命中cell.accessoryType=UITableViewCellAccessoryCheckmark行。@MayYang,那么RetrievedIndExpath一定有问题。日志显示了什么?2015-04-04 16:49:43.494 speeddate[1065:19761](“{length=2,path=0-2}”,“length=2,path=0-3}”)@MayYang我无法从打印出来的内容判断你是否有打字错误——它在开头显示了两个括号,在结尾只显示了一个。哪一个是正确的?我检查了一下,最后还有一个)。因此,开头有2个圆括号,结尾有2个圆括号。