views:

70

answers:

1

I'm using code largely from cimgf to update the order of my rows in a UITableViewController table that is displayed through pushViewController. The sample code works great, but in my project, when I move the rows, they disappear, get screwed up, or get stuck (eg: http://sites.google.com/site/basicmjc/home/uiviewcorrupt.png).

Below is some relevant code:

Can anyone tell me what's going on?

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{

    NSMutableArray *things = [[self.fetchedResultsController fetchedObjects] mutableCopy];

    // Grab the item we're moving.
    NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];

    // Remove the object we're moving from the array.
    [things removeObject:thing];

    // Now re-insert it at the destination.
    [things insertObject:thing atIndex:[destinationIndexPath row]];

    // All of the objects are now in their correct order. Update each
    // object's displayOrder field by iterating through the array.
    int i = 0;
    for (NSManagedObject *mo in things)
    {
        NSLog(@"%i - %@", i, [mo valueForKey:@"acctAddr"]);
        [mo setValue:[NSNumber numberWithInt:i++] forKey:@"sortOrder"];
    }
    [things release], things = nil;

    // Save
 NSError *error = nil;
    if (![self.managedObjectContext save:&error]) 
    {
        NSString *msg = @"An error occurred when attempting to save your user profile changes.\nThe application needs to quit.";
        NSString *details = [NSString stringWithFormat:@"%@ %s: %@", [self class], _cmd, [error userInfo]];
        NSLog(@"%@\n\nDetails: %@", msg, details);
    }

    // re-do the fetch so that the underlying cache of objects will be sorted
    // correctly

    if (![self.fetchedResultsController performFetch:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
 if (editingStyle == UITableViewCellEditingStyleDelete)
 {
  NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
  [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

  NSError *error = nil;
  if (![context save:&error])
  {
   NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
   abort();
  }
 }   
}


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    NSString *tempStr;

    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    //cell.textLabel.text = [[managedObject valueForKey:@"acctAddr"] description];
 tempStr = [[managedObject valueForKey:@"acctFriendlyName"] description];
 if ([tempStr length] < 1) {
  tempStr = [[managedObject valueForKey:@"acctAddr"] description];
 }
 else cell.detailTextLabel.text = [[managedObject valueForKey:@"acctAddr"] description];

 cell.textLabel.text = tempStr;

 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

}
A: 

Aha, looks as if the answer is here