Save new cell position after reordered and affect model (datasource)

Asked

Viewed 39 times

0

I need to implement a tableview, where I can reorder the position of the cells and save them in the new order. And this new order should affect the order they are saved also the model (in this case a Nsmutablearray or Nsmutabledictionry).

I will put image below to illustrate better:

Posição Inicial

Modo de Edição

Mudança na Ordem das Células

Nova ordem que preciso salvar

1 answer

1

In the @interface that implements the UITableViewDataSource (geralmente oview controller associated with the table), you need to implement the selector tableView:moveRowAtIndexPath:toIndexPath:. In the implementation you need to change the order of your elements NSMutableArray, as in the example below:

- (void)   tableView:(UITableView *)tableView
  moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
         toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSInteger fromIndex = sourceIndexPath.row;
    NSInteger toIndex = destinationIndexPath.row;
    if (fromIndex == toIndex) return;

    id item = [self.items objectAtIndex:fromIndex];
    [self.items removeObjectAtIndex:fromIndex];
    [self.items insertObject:item atIndex:toIndex];
}
  • Carlos I had something similar to what you presented. But I will test your code and report the result!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.