Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move row inside a UITableView

I have a UITableView that contain 3 sections, It is possible in the edit mode to change the row position just inside his section and not to another section?Actually I can move the cell to any section.

Thanks

like image 805
Maxime Avatar asked Dec 08 '25 12:12

Maxime


2 Answers

Many correct answers but no one presented a complete and well presented reply.

Use the table view delegate method tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: as shown below.

- (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
}

If I understand your question correctly, then yes: use

- (NSIndexPath*) tableView: (UITableView*) tableView targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath*) sourceIndexPath toProposedIndexPath: (NSIndexPath*) proposedDestinationIndexPath
like image 26
Steven Kramer Avatar answered Dec 10 '25 03:12

Steven Kramer