Rebooting the table view every time you want to add or remove a row creates a bad experience for the user of your application. Although this is not an effective way to accomplish this task, it also has some negative side effects - the selected rows do not remain selected after a reboot and the changes are not animated.
UITableView have methods that were created to dynamically change the contents of a table view. It:
insertRowsAtIndexPaths:withRowAnimation: moveRowAtIndexPath:toIndexPath: deleteRowsAtIndexPaths:withRowAnimation:
Please note that these methods allow you to specify the type of animation that will be used when performing the specified operation - you cannot achieve this behavior when you use reloadData to change the contents of the table view.
In addition, you can combine several operations with a table view using additional table view methods (this is optional):
beginUpdates endUpdates
Itβs just that the transfer operations you want to perform into the calls to beginUpdates and endUpdates and the table view will create one animation for all the operations requested between the calls to beginUpdates and endUpdates so that the whole transition looks better created by several separate animations.
[self.tableView beginUpdates] //calls to insert/move and delete methods [self.tableView endUpdates]
It is very important that your data source state matches the one stored in the UITableView . For this reason, you must ensure that when the table view begins to perform the requested operations, its data source will return the correct values.
[self.tableView beginUpdates] //calls to insert/move and delete methods //operations on our data source so that its //state is consistent with state of the table view [self.tableView endUpdates]
When does a table view begin to perform operations? It depends on whether the operations in the animation block are defined by the beginUpdates and endUpdates . If so, the table view begins operations after calling the endUpdates method. Otherwise, the table view performs operations immediately after a call to the insert / move or delete method has been made.
When you use the beginUpdates and endUpdates to perform operations on a table view, you need to know that in this case the table view, the package requests the operations and executes them in a certain order, which is not necessarily the same as the order of the calls made by you on your table viewer ( Apple documentation on this ).
The most important thing to remember is that deleting all operations is always performed before all insert operations. In addition, the delete operations are apparently performed in descending order (operations for indices 3, 2, 1), when the insert operation is performed in ascending order (operations for indices 1, 2, 3). Remember that it is important to maintain the state of your data source in accordance with the saved table view.
Spend some time analyzing the order of operations with the data source and the table view presented in the example below.
The final example:
//initial state of the data source self.numbers = [@[@(0), @(1), @(2), @(3), @(4), @(5), @(6)] mutableCopy]; // //... // NSArray indexPathsToRemove = @[[NSIndexPath indexPathForRow:3 section:0]. [NSIndexPath indexPathForRow:0 section:0]; NSArray indexPathsToAdd = @[[NSIndexPath indexPathForRow:6 section:0], [NSIndexPath indexPathForRow:5 section:0]]; [self.tableView beginUpdates]; [self.numbers removeObjectAtIndex:3]; [self.numbers removeObjectAtIndex:0]; [self.numbers insertObject:@(10) atIndex:4]; [self.numbers insertObject:@(11) atIndex:5]; [self.tableView insertRowsAtIndexPaths:indexPathsToAdd withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView endUpdates]; //final state of the data source ('numbers') - 1, 2, 4, 5, 6, 10, 11