iTahDoodle - Removing a task via swipe-to-delete protocol


#1

I am sure this is covered in more detail on your iOS book but, I have been playing with the following method from the UITableViewDataSource protocol in an attempt to incorporate task deletion with swipe animaton.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

However, though it allows me to swipe to the left and it reveals the delete button, it seems to catch an exeption everytime I hit Delete (obviously I am not implementing the method properly). Can anyone give some insight? I plan to go through the iOS book eventually but would be nice to know what I could be doing wrong…

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *deleteIndexPaths = [NSArray arrayWithObjects:indexPath, nil]; [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; [self.taskTable reloadData]; [self.tasks removeObjectAtIndex:indexPath.item]; }


#2

Funny, I just figured out my problem!. The exception dealt with the number of rows left on the table not matching what was set originally after the item deletion. I added a line to update the ‘numberOfRowsInSection’ method after deleting the item from the table AND from the array (the derive the new count). This seems to work! still welcome any pointers.

FIX:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *deleteIndexPaths = [NSArray arrayWithObjects:indexPath, nil]; [self.taskTable beginUpdates]; [self.taskTable deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; [self.tasks removeObjectAtIndex:indexPath.item]; [self.taskTable numberOfRowsInSection:[self.tasks count]]; // <--- update table row count after deletion [self.taskTable endUpdates]; [self.taskTable reloadData]; }