Implement re-ordering of UITableView with simple Core Data app

So lets say i have a simple core data app with 1 entity called Girl and 2 attributes; name(string) and displayOrder(integer). So to allow the user to re-order the table rows, i think i have to implement 2 methods canMoveRowAtIndexPath and moveRowAtIndexPath. The latter is easy enough:

// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}

Open in new window


then the moveRowAtIndexPath method. to fetch all the objects from the persistent i use a NSMutableArray called girls

// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    
    [self.girls exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];

    [self.tableView reloadData];

    // so i guess after the re-ordering i need to save it? not sure how

}

Open in new window

depassionAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

darbid73Commented:
I think/hope you are after confirmation how this works.  I think you are on the right track.

I think it is a 2 step process, First handle and code the movements and then implement the persistence with Core Data and NSFetchedResultsController.

Please refer to these to check;

Apple - re-ordering uitableview

Tables - Part III - re-ordering the table.

Re-ordering

Add - Remove a cell.
0
depassionAuthor Commented:
thats great and all, but none of that really relates to Core Data and saving the state of the array after the re-ordering.
0
darbid73Commented:
I was wondering if we would get to a question.

So are you using a NSFetchedResults Controller?  If not why not?
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

depassionAuthor Commented:
i'm not using NSFetchedResultsController, just a NSMutableArray. I wanted the first iteration to as simple as possible.

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // fetch all the objects from the persistent store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Girl"];
    self.girls = [[managedObjectContext executeFetchRequest:fetchRequest error:nil]mutableCopy];
    
    [self.tableView reloadData];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    NSManagedObject *girl = [self.girls objectAtIndex:indexPath.row];
    [cell.textLabel setText:[girl valueForKey:@"name"]];
    [cell.detailTextLabel setText:[girl valueForKey:@"age"]];
    return cell;
}

Open in new window

0
depassionAuthor Commented:
ok lets say we use the CoreDataBooks sample app from Apple to demonstrate re-ordering cells. Does anyone have a solid example of how to do it?
0
darbid73Commented:
I am pretty sure that I have given you both examples from websites and theory from Apple on re-ordering cells.

By the way here is something on NSFetchedResultsController

See this as part of a series of tutorials on the NSFetchedResultsController

If you follow the above and the suggestions in my other post, then to persist the data you save the ManagedObjectContext.

See there first part of this tutorial on saving the data.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
iOS

From novice to tech pro — start learning today.