steve jobs
steve jobs

Reputation: 97

UITableView with Dragging

I have an iPad application with UITableView, and I want to enable a user to reorder the table items by dragging the cell and move it before or after another cell, how I can do that ?

Tanks

Upvotes: 0

Views: 333

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

Implement moveRowAtIndexPath:toIndexPath: method in your data shource to update the model on the end of the drag operation. Implement tableView:canMoveRowAtIndexPath: to return YES for the rows that the user should be able to move. Here is a short tutorial on the subject.

Upvotes: 2

Nick Lockwood
Nick Lockwood

Reputation: 40995

You need to implement these dataSource methods in your view controller:

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

and

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //update your model to reflect the fact that the specified rows indexes have been swapped
}

Upvotes: 2

Related Questions