jwknz
jwknz

Reputation: 6824

Core Data - Reordering TableView won't save

.

Hello,

I have the following code to set the re-ordering of the tableView.

#pragma mark Row reordering
// Determine whether a given row is eligible for reordering or not.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// Process the row move. This means updating the data model to <span id="IL_AD6" class="IL_AD">correct</span> the item indices.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
  toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [arr objectAtIndex:fromIndexPath.row];
[arr removeObject:item];
[arr insertObject:item atIndex:toIndexPath.row];


}

But when I reload the TableView (note not a TableViewController) The order is set back to what it was before I changed anything.

Now when I setup the editing for this view (the delete buttons) I had to add this line:

[self.mainTableView setEditing:YES animated:YES];

before it appeared in my view.

Is there a similar thing I need to set with the re-ordering of the table view for it to save?

The data is fetched from a Core Data database.

Upvotes: 0

Views: 675

Answers (2)

Mundi
Mundi

Reputation: 80271

In order to persist the order of the objects I'm afraid you will need to add an attribute for that to your data model. Just use an int / [NSNumber] and recompute them in a loop when a table view row is moved.

Upvotes: 1

LJ Wilson
LJ Wilson

Reputation: 14427

When you reload the data, are you re-populating that array with the result of a fetch from your Core Data entity? If so, the changes you made to the array (which I assume is the data source for the TV) will be blown away. If you want those changes to stick, you would need to update the model somehow to make sure the re-population of the array results in the array being indexed the way you need.

Upvotes: 0

Related Questions