Search code examples
iphoneiosuitableviewnsindexpath

What is the index path of a table view cell that is currently being rearranged (moved) by the user?


When a table view is in editing mode and the user is moving cells around. What is the index path of the cell currently being moved? How are the index paths of the remaining cells affected?

(I'm actually trying to learn this in order to solve this problem.)

Any help is appreciated. Thanks!


Solution

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

    Do not forget to do another check in your tableView:moveRowAtIndexPath:toIndexPath: data source method as shown below, you do not want to run your application logic when the destination is similar to the source.

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath      *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {
    if(sourceIndexPath == destinationIndexPath)
        {
        return;        
        }
    
    //application logic
    }