The number of rows in your table [[ToDoItemSvc retrieveAllToDoItems] count] . When you delete row 1 in your table, the number of rows in your table should be 1 less than the number of rows before deleting any rows. After deleting row 1 and calling [self.tableView reloadData] tableView checks how many rows are in the table. At this point, numberOfRowsInSection will return [[ToDoItemSvc retrieveAllToDoItems] count] . Now it should be 1 less than it was before the row was deleted.
Short answer: first you need to remove the item from your data source that looks like [ToDoItemSvc retrieveAllToDoItems] , and then delete the row.
In addition to this, when you add a row, you need to add an element to your data source.
These changes must occur before calling reloadData .
Edit
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // Actually remove the data from the source [ToDoItemSvc deleteToDoItem:[ToDoItemSvc retrieveAllToDoItems][indexPath.row]] [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView reloadData]; }
ELI5: The teacher has five students: Alice, Bob, Charlie, Diana and Eric. Bob's mom picks him up early from school before lunch. After lunch, the teacher takes part and panic, because he has only four children, when the list says that there should be five. Where is bob ?!
If Bob's mom removed his name from the list when she pulled him out of school, then the teacher would not panic.
Fennelouski
source share