Patrik Krupička
Patrik Krupička

Reputation: 11

When I am moving cells in UITableView, I can only drag last but one cell to only last position

I have a problem with my UITableViewController. I have list of objects that I want to move.

The thing is that I am able only to move the last of one cell to the last position, not any other of cells. I think it is just simple code, but I really do not know, what I am missing.

I do not want to delete or insert objects, I just want to move them. Thanks for any help.

Here is my code:

myListOfObjects is NSMutableArray of my custom objects

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView registerCellClassWithNib:[MyCustomCell class]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView setEditing:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.myListOfObjects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyCustomCell className]
                                                          forIndexPath:indexPath];

    cell.myObject = self.myListOfObjects[indexPath.row];

    return cell;
}

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

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    [self.myListOfObjects exchangeObjectAtIndex:toIndexPath.row withObjectAtIndex:fromIndexPath.row];
}

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

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

Upvotes: 1

Views: 306

Answers (2)

Satish Azad
Satish Azad

Reputation: 2312

Try to use following TableView DataSource method for editing:

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
       toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
   /**
      Add your condition here if any
    */

 // Allow the proposed destination.
    return proposedDestinationIndexPath;
}

And Exchange your objects in following delegate method:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { 
/**
  Exchange the index of two objects
*/

[self.YourDataObjectArr exchangeObjectAtIndex:toIndexPath.row withObjectAtIndex:fromIndexPath.row];

}

Upvotes: 0

Thomás Pereira
Thomás Pereira

Reputation: 9768

Why not use remove and insert objects in self.myListOfObjects. All examples that I saw about tableView:moveRowAtIndexPath use this approach.

Just try:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    MyObject *myObject = [self.myListOfObjects objectAtIndex:fromIndexPath.row];
    [self.myListOfObjects removeObject:myObject];
    [self.myListOfObjects insertObject:myObject atIndex:toIndexPath.row];
}

Instead of:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    [self.myListOfObjects exchangeObjectAtIndex:toIndexPath.row withObjectAtIndex:fromIndexPath.row];
}

Upvotes: 1

Related Questions