views:

273

answers:

2

Hello all, Ive been working on a list app where core data is used to persist a checkmark Everything was fine till xcode crashed. No code was changed but now instead of a checkmark showing up immediatly you have to click the row, quit the app, then relaunch it to get anything. This is the code along with an other question of how I got the code in the first place. http://stackoverflow.com/questions/2215465/how-can-a-checkmark-state-be-saved-in-core-data

Any help is greatly appreciated. Thanks

- (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;
}
A: 

Hi

Does your core data model have a "default value"? Maybe it has been set to "NO" instead of "YES".

RickiG
Hello, its set to neither of those. Last time it worked perfectly. I dont know whats changed since the crash other than the code hasnt.
Tanner
+2  A: 

Have you implemented the necessary NSFetchedResultsControllerDelegate methods to catch updates to your data model? If not, your table won't know to update the row's visual state. Some boilerplate code for this is as follows:

- (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];
}
Brad Larson
Yes, turns out there is a bug in the newest xcode that gives an assertion error. To fix it I simply ran the app on the old xcode.
Tanner