2

I know this question has been asked before, However, I'm still confused as to how to implement reordering with a UITableView cells in a Core Data project.Below mentioned code i have used in my project for rearranged the TableView cells. After rearranged cells not affected in core data.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
      toIndexPath:(NSIndexPath *)toIndexPath
{
 NSInteger sourceRow = fromIndexPath.row;
        NSInteger destRow = toIndexPath.row;
        Question *cat1=[questionsArray objectAtIndex:sourceRow];
        [questionsArray removeObjectAtIndex:sourceRow];
        [questionsArray insertObject:cat1 atIndex:destRow];
        [self.cardsTable setEditing:NO animated: YES];
        [cardsTable reloadData];
}
VasuIppili
  • 37
  • 6

2 Answers2

3

If you can target iOS 5.0 and above, then you could use an NSOrderedSet to maintain the order of your objects. Please keep in mind that using this method is significantly less efficient than the other method I am suggesting below (as per Apple's documentation). For more information, please check the Core Data Release Notes for iOS 5.

If you need to support iOS versions before 5.0 or would like to use a much more efficient approach, then you should create an additional integer attribute in your entity and manually maintain the index of the entity's objects in it as they are being added or rearranged. When it's time to display the objects, you should sort them based on this new attribute and you're all set. For example, this is how your moveRowAtIndexPath method should then look like:

- (void)moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath sortProperty:(NSString*)sortProperty
{
    NSMutableArray *allFRCObjects = [[self.fetchedResultsController fetchedObjects] mutableCopy];

    NSManagedObject *sourceObject = [self.fetchedResultsController objectAtIndexPath:sourceIndexPath];

    // Remove the object we're moving from the array.
    [allFRCObjects removeObject:sourceObject];
    // Now re-insert it at the destination.
    [allFRCObjects insertObject:sourceObject atIndex:[destinationIndexPath row]];

    // Now update all the orderAttribute values for all objects 
    // (this could be much more optimized, but I left it like this for simplicity)
    int i = 0;
    for (NSManagedObject *mo in allFRCObjects)
    {
        // orderAttribute is the integer attribute where you store the order
        [mo setValue:[NSNumber numberWithInt:i++] forKey:@"orderAttribute"];
    }
}

Finally, if you find this too much manual work, then I really recommend using the free Sensible TableView framework. The framework will not only maintain the order automatically for you, it will also generate all the table view cells based on your entities' attributes and its relationships with other entities. Definitely a great time saver in my opinion. I also know of another library called UIOrderedTableView, but I've never used that myself so I can't recommend it (the former framework is also much more popular).

Matt
  • 2,391
  • 2
  • 17
  • 18
0

First you have to keep the index in to your NSManagedObject.

When you in insert a new object make sure you set the index to be lastIndex+1

After you fetch for the objects sort by your index.

When you reorder the cells you also have to set the indexes to the objects. Pay attention because you might have to change the indexes to all objects.

Example: You take the first cell and move it to last position. In this case all your indexes are changed. The FirstCell takes last index and all others take oldIndex-1.

I sugest after done editing iterate to your data source array and just set the object index to the iteration index.

Alex Terente
  • 12,006
  • 5
  • 51
  • 71