I have not been able to find an obvious way to do this in a way that causes the rows to move while the drag is in progress.
However, I do have something that should work once the drag is complete. However, for some reason I don't understand, the display is screwed up when the drag completes. Here is how I'm doing it:
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case INDEX_OF_SECTION_SELECTED_READOUTS:
return NUMBER_OF_READOUTS_DISPLAYED;
break;
case INDEX_OF_SECTION_UNSELECTED_READOUTS:
return NUMBER_OF_POSSIBLE_READOUT_CHOICES - NUMBER_OF_READOUTS_DISPLAYED;
break;
default:
return 0;
break;
}
}
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case INDEX_OF_SECTION_SELECTED_READOUTS:
return @"Displayed";
break;
case INDEX_OF_SECTION_UNSELECTED_READOUTS:
return @"Available";
break;
default:
return @"";
break;
}
}
- (int) getIndexFromIndexPath:(NSIndexPath *)indexPath {
int index = indexPath.row;
if (indexPath.section == INDEX_OF_SECTION_UNSELECTED_READOUTS) {
index += NUMBER_OF_READOUTS_DISPLAYED;
}
return index;
}
- (void) tableView:(UITableView *) tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
int fromIndex = [self getIndexFromIndexPath:fromIndexPath];
int toIndex = [self getIndexFromIndexPath:toIndexPath];
if (toIndex < fromIndex) {
// Shuffle the to row and everything beneath it down by one
for (int i = fromIndex; i > toIndex; i--) {
[readouts exchangeObjectAtIndex:i withObjectAtIndex:i-1];
}
}
else {
// Shuffle the to row and everything above it up by one
for (int i=fromIndex; i < toIndex; i++) {
[readouts exchangeObjectAtIndex:i withObjectAtIndex:i+1];
}
}
// Now update the order for the readouts
for (int i=0; i < [readouts count]; i++) {
NSString *key = [readouts objectAtIndex:i];
[readoutService setReadoutType:key index:i];
}
}
This should be right, but when the drag is complete, cells go totally missing from the display (with only the background visible where the cell should be).
I've tried calling [self.tableview reloadData] from within moveRowAtIndexPath, but Apple's documentation says "It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates"
I'm stuck on this and seeking input on how to fix the display issue.