7

I'm trying to have UITableView with 2 classes of UITableViewCell (section 0 with one row, section 1 with [self.dataArray count] rows).

I tried these links: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageReorderRow/ManageReorderRow.html

reordering control in UITableView
Reorder control isn't displayed on table view

I've added:

    -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{   

    // Get the object we are going to move
    Task *task = [[Task alloc] init];
    task = [self.taskArray  objectAtIndex:[destinationIndexPath row]];
    
    // We will delete and move it to another location so well have to retain it
    //[task retain];
    
    // Remove it an move it to other place
    [self.taskArray removeObjectAtIndex:[sourceIndexPath row]];
    [self.taskArray insertObject:task atIndex:[destinationIndexPath row]];
}


     // Override to support conditional rearranging of the table view.
     - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
         if (indexPath.section == 1) {
             return YES;
         }
         return YES;
     }

And I did add for every UITableViewCell class:

cell.showsReorderControl = YES;

but still no luck, no reaorderControl is showing... anybody? This is quite urgent and annoying to have so many tutorials, but not working for me.

EDIT:

I changed a bit topic to include fact, that I was using UIViewController not UITableViewController.

Community
  • 1
  • 1
raistlin
  • 4,298
  • 5
  • 32
  • 46

2 Answers2

16

To implement row reordering in a UITableView, you need to implement tableView:canMoveRowAtIndexPath: and tableView:moveRowAtIndexPath:toIndexPath:, You might also want to implement tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:.

And the most important, the table view must be in edit mode for the reorder controls to appear.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • I've added to my navigationItem.rightbuttons `[self editButtonItem];` but pressed didn't initiate drag controller to appear - any idea? I don't want to have table set all the time to edit mode – raistlin Nov 20 '12 at 00:11
  • I mean, beside constructing custom button with atached method - is there a proper or better way to achieve that? – raistlin Nov 20 '12 at 00:12
  • 1
    Adding `[self editButtonItem]` is good assuming you are using a `UITableViewController` and not a `UIViewController`. Are you overriding the `setEditing:animated:` method by any chance? If you are, make sure you call `[super setEditing:editing animated:animated]'`. – rmaddy Nov 20 '12 at 02:51
  • thanks, you saved my life :) it looks like every one here on SO was using UITableViewController, so they didn't add this little detail :) – raistlin Nov 20 '12 at 09:27
  • 2
    You can still use `[self editButtonItem]` in a `UIViewController`. You just need to make sure that you override `setEditing:animated:` to include a call to `[self.tableView setEditing:editing animated:animated]`. – rmaddy Nov 20 '12 at 15:22
  • Like he said: if you are not using a UITableViewController, you must manually call the self.tableView.setEditing() method inside the self.setEditing() method. (Note same method name for both controller and view!) – Andrew Duncan Apr 17 '15 at 17:40
  • This is the key ingredient I was missing: `the table view must be in edit mode for the reorder controls to appear` – Starsky Sep 29 '20 at 15:25
0

try this . . .this will handle arranging and updating of cell in case of simple tableview in editing mode

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

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
     [tableData insertObject: [tableData objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath.row];
     [tableData removeObjectAtIndex:(sourceIndexPath.row + 1)];
}
Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70