views:

128

answers:

1

I've enabled editing mode and moving cells around to allow users to position table view content in the order they please. I'm using Core Data as the data source, which sorts the content by the attribute "userOrder". When content is first inserted, userOrder is set to a random value. The idea is that when the user moves a cell around, the userOrder of that cell changes to accomodate its new position. The following are problems I am running into while trying to accomplish this:

  1. Successfully saving the the new location of the cell and adjusting all changed locations of influenced cells.

  2. Getting the data to be consistent. For example, the TableView handles the movement fine, but when i click on the new location of the cell, it displays data for the old cell that used to be that location. Data of all influenced cells gets messed up as well.

I know I have to implement this in:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {}

I just don't know how. The apple docs are not particularly helpful if you are using Core Data, as in my situation.

Any guidance greatly appreciated!

A: 

Problem 1 is pretty much independent of using CoreData, just think of a simple array:

 {0 1 2 3 4 A}. 

Now the user moves some freshly appended element A within the Array:

 {0 1 A 2 3 4}

So starting with the inserted element A, which receives the index 2, all elements need to have their index (which is userOrder in your case) incremented by one. So you map 0->0, 1->1, A->2, 2->3, 3->4 and so on.

Problem 2 may have two or three sources:

  1. Either your CoreData values are not synced so the database is not in sync with what you see.
  2. [table reloadData] was not called
  3. Your move-Algorithm is wrong

So perhaps you can check the thee points above and if none applies you could consider posting your move-algorithm for closer examination.

Jens