I have an iPhone app and I'm trying to implement table row reordering and to 
still have it work with Core Data.

Here's my code:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath 
*)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
        if (fromIndexPath.section == toIndexPath.section) {
                NSMutableArray *objectArray = [[[self fetchedResultsController] 
fetchedObjects] mutableCopy];
                NSManagedObjectContext *context = [[self 
fetchedResultsController] managedObjectContext];
                NSFetchedResultsController *frc = [self 
fetchedResultsController];
                NSManagedObject *objectToMove = [[objectArray 
objectAtIndex:fromIndexPath.row] retain];
                [objectArray removeObjectAtIndex:fromIndexPath.row];
                [objectArray insertObject:objectToMove atIndex:toIndexPath.row];
                [objectToMove release];
        
                for (int i=0; i<[objectArray count]; i++)
                {
                        [(NSManagedObject *)[objectArray objectAtIndex:i] 
setValue:[NSNumber numberWithInt:i] forKey:@"displayOrder"];
                }
                [objectArray release];
                
                NSError *error;
                BOOL success = [context save:&error];
                if (!success)
                {
                        UIAlertView *errorAlert = [[[UIAlertView alloc] 
initWithTitle:@"Error encountered while retrieving data." message:nil 
delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] autorelease];
                        [errorAlert show];
                        NSLog(@"Unresolved error %@, %@", error, [error 
userInfo]);
                }
                
                success = [frc performFetch:&error];
                if (!success)
                {
                        UIAlertView *errorAlert = [[[UIAlertView alloc] 
initWithTitle:@"Error encountered while retrieving data." message:nil 
delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] autorelease];
                        [errorAlert show];
                        NSLog(@"Unresolved error %@, %@", error, [error 
userInfo]);
                }
        }

I have an integer attribute in my entity called "displayOrder" that keeps track 
of the order in which the items should appear, and I have it set to sort using 
that key. In the above method I just update the displayOrder attribute for all 
objects, save the context, and perform a fetch again (to make sure that 
internally, everything is ordered the way the user rearranged the items)

The issue is that the above code doesn't seem to work at all. I move a row, and 
all sorts of UI chaos occurs. The row before the row I move disappears, the row 
I move takes its place, and everything just messes up altogether. What am I 
doing wrong here?

Thanks


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to