views:

13

answers:

1

I have a grouped table that I want the user to be able to reorder. I would like to have two groups. When I move a row from group A, to group B, I would like the top row from group B to be moved to the bottom of group A. When I move a row from group B to group A, I would like the bottom row of group A to be moved to the top of Group B.

This way the size of each group in the table is maintained. Is this possible?

Thank you very much!

A: 

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.

benvolioT
I let the user drag the cells wherever they want and then when they press done, it separates the list into the top six and the bottom four. This is the closest I could come to keeping them separated. But it looks fine. Thanks for your help.
Rossi