moveRowAtIndexPath - Moving cells between sections

ios, objective-c, uitableview

Solution

Your problem is that you have manipulated the data model, but you haven't told the tableview about the changes you made.

Because you call `beginUpdates`/`endUpdates` the tableview is expecting some changes, so after `endUpdates` it calls `numberOfRowsInSection` - which returns the answer '3' - but it is expecting 2+0 (because you didn't tell it about the new row).

You have a couple of options:

1 - Use `moveRowAtIndexPath:toIndexPath:`

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    if (fromIndexPath != toIndexPath ) {

        Department *department = [_objects objectAtIndex:fromIndexPath.section];
        Employee *employee = [department.employees objectAtIndex:fromIndexPath.row];

        [tableView beginUpdates];
        [department.employees removeObjectAtIndex:fromIndexPath.row];
        [department.employees insertObject:employee atIndex:toIndexPath.row];
        [tableview moveRowAtIndexPath:fromIndexPath toIndexPath:toIndexPath];
        [self.tableView endUpdates];
    }
}

2 - Use reload without the `beginUpdate`/`endUpdate`

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    if (fromIndexPath != toIndexPath ) {

        Department *department = [_objects objectAtIndex:fromIndexPath.section];
        Employee *employee = [department.employees objectAtIndex:fromIndexPath.row];

        [department.employees removeObjectAtIndex:fromIndexPath.row];
        [department.employees insertObject:employee atIndex:toIndexPath.row];
        [tableView reloadData];
    }
}

Problem

In my UITableView I have this said relationship `Department -< Employees` (array of names) I have set up custom objects for each model. In my `moveRowAtIndexPath`, I can move in a specific section; however it crashes if I try to move it between sections. The idea being I want to be able to move "Tom" from "Sales" to "Marketing" ``` #pragma mark - UITableView delegate -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return [_objects count]; } -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { Department *department = [_objects objectAtIndex:section]; return [department.employees count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId" forIndexPath:indexPath]; // Configure the cell... Department *department = [_objects objectAtIndex:indexPath.section]; Employee *employee = [department.employees objectAtIndex:indexPath.row]; cell.textLabel.text = employee.name; return cell; } -(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { Department *department = [_objects objectAtIndex:section]; return department.name; } - (BOOL) tableView: (UITableView *) tableView canEditRowAtIndexPath: (NSIndexPath *) indexPath { return YES; } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { if (fromIndexPath == toIndexPath ) return; Department *department = [_objects objectAtIndex:fromIndexPath.section]; Employee *employee = [department.employees objectAtIndex:fromIndexPath.row]; [self.tableView beginUpdates]; [department.employees removeObjectAtIndex:fromIndexPath.row]; [department.employees insertObject:employee atIndex:toIndexPath.row]; [self.tableView endUpdates]; [tableView reloadData]; } ``` How do I get the `moveRowAtIndexPath` to allow me to move employees from one department to another (or rather one section to another)? Many thanks // Crash below; ``` *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:1368 2015-02-20 20:51:15.772 Departments[999:90b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' *** First throw call stack: ( 0 CoreFoundation 0x01a221e4 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x017a18e5 objc_exception_throw + 44 2 CoreFoundation 0x01a22048 +[NSException raise:format:arguments:] + 136 3 Foundation 0x013814de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 4 UIKit 0x00539f63 -[UITableView _endCellAnimationsWithContext:] + 13402 5 UIKit 0x00549cea -[UITableView endUpdatesWithContext:] + 51 6 UIKit 0x00549d18 -[UITableView endUpdates] + 41 7 Departments 0x00003a48 -[ViewController tableView:moveRowAtIndexPath:toIndexPath:] + 664 8 UIKit 0x005573b4 -[UITableView _endReorderingForCell:wasCancelled:animated:] + 619 9 UIKit 0x006e0c4f -[UITableViewCell _grabberReleased:] + 73 10 UIKit 0x007d1a1b -[UITableViewCellReorderControl endTrackingWithTouch:withEvent:] + 58 11 UIKit 0x005641f1 -[UIControl touchesEnded:withEvent:] + 559 12 UIKit 0x004a2ddd -[UIWindow _sendTouchesForEvent:] + 852 13 UIKit 0x004a39d1 -[UIWindow sendEvent:] + 1117 14 UIKit 0x004755f2 -[UIApplication sendEvent:] + 242 15 UIKit 0x0045f353 _UIApplicationHandleEventQueue + 11455 16 CoreFoundation 0x019ab77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 17 CoreFoundation 0x019ab10b __CFRunLoopDoSources0 + 235 18 CoreFoundation 0x019c81ae __CFRunLoopRun + 910 19 CoreFoundation 0x019c79d3 CFRunLoopRunSpecific + 467 20 CoreFoundation 0x019c77eb CFRunLoopRunInMode + 123 21 GraphicsServices 0x031ee5ee GSEventRunModal + 192 22 GraphicsServices 0x031ee42b GSEventRun + 104 23 UIKit 0x00461f9b UIApplicationMain + 1225 24 Departments 0x000046bd main + 141 25 libdyld.dylib 0x02ed5725 start + 0 26 ??? 0x00000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) ```

Original source