• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Core Data: UITableView moveRowAtIndexPath
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Core Data: UITableView moveRowAtIndexPath


  • Subject: Core Data: UITableView moveRowAtIndexPath
  • From: PCWiz <email@hidden>
  • Date: Wed, 3 Mar 2010 22:23:05 -0700

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 (email@hidden)

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:

This email sent to email@hidden

  • Prev by Date: Re: NSImageRep simple code
  • Next by Date: Re: Returning a useful error from NSDocument's -readFromURL:...
  • Previous by thread: Re: Sorting contents of an NSCollectionView
  • Next by thread: NSSegmentedControl select multiple cells
  • Index(es):
    • Date
    • Thread