为什么存储在Core Data中的布尔值不会立即显示?

时间:2010-02-08 20:03:48

标签: iphone cocoa-touch core-data

我一直在开发一个列表应用程序,其中核心数据用于保持一个复选标记一切都很好,直到xcode崩溃。没有代码被更改,但现在而不是立即显示复选标记,您必须单击该行,退出应用程序,然后重新启动它以获取任何内容。这是代码以及我如何获得代码的另一个问题。   How can a checkmark state be saved in core data?

非常感谢任何帮助。感谢

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSManagedObject *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];


    if ([[selectedObject valueForKey:@"check"] boolValue]) {
        [selectedObject setValue:[NSNumber numberWithBool:NO] forKey:@"check"];
    } else {
        [selectedObject setValue:[NSNumber numberWithBool:YES] forKey:@"check"];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *defaultCellIdentifier = @"Item";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultCellIdentifier] autorelease];
    }

    NSManagedObject *item = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    cell.textLabel.text = [item valueForKey:@"name"];

    if ([[item valueForKey:@"check"] boolValue]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}



-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accesoryTypeForRowWithInddexPath:(NSIndexPath *)indexPath {
    return UITableViewCellAccessoryNone;
}

2 个答案:

答案 0 :(得分:2)

您是否已实施必要的NSFetchedResultsControllerDelegate方法来捕获数据模型的更新?如果没有,您的表将不知道更新行的可视状态。一些样板代码如下:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{
    [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type 
{   
    switch(type) 
    {
        case NSFetchedResultsChangeInsert:
        {
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        }; break;

        case NSFetchedResultsChangeDelete:
        {
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        }; break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath 
{   
    switch(type) 
    {
        case NSFetchedResultsChangeInsert:
        {
            // This works around problems with inserting a row at the beginning of the list
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(newIndexPath.row + 1) inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
        }; break;

        case NSFetchedResultsChangeDelete:
        {
            if (self.tableView.editing)
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
            else
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }; break;

        case NSFetchedResultsChangeUpdate:
        {
            [self updateCell:[self.tableView cellForRowAtIndexPath:indexPath] fromCategory:[fetchedResultsController objectAtIndexPath:indexPath]];
        }; break;

        case NSFetchedResultsChangeMove:
        {
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        }; break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{
    [self.tableView endUpdates];
}

答案 1 :(得分:0)

您的核心数据模型是否具有“默认值”? 也许它被设置为“NO”而不是“YES”。