How to prevent a UITableViewCell being moved away from its own section?

ios, iphone, objective-c, uitableview

Solution

I think this will solve your problem

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if( sourceIndexPath.section != proposedDestinationIndexPath.section )
        return sourceIndexPath;
    else
        return proposedDestinationIndexPath;
}

From Apple documentation

This method allows customization of the target row for a particular row as it is being moved up and down a table view. As the dragged row hovers over a another row, the destination row slides downward to visually make room for the relocation; this is the location identified by proposedDestinationIndexPath.

Problem

My `UITableView` has 3 sections. Only the cells in the 2nd sections are movable: ``` - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { BOOL canMove = NO; if (indexPath.section == 1) { canMove = YES; } return canMove; } ``` However, a cell (`B`) in the 2nd section (originally contains `A``B``C` cells) can be moved to the 1st section (originally contains only the `T` cell): How can I make sure cells are moved within its own section?

Original source