Friday, June 7, 2013

UITableView deleteRowsAtIndexPaths withRowAnimation example in Objective C (iOS).


UITableView deleteRowsAtIndexPaths withRowAnimation

Deletes the rows specified by an array of index paths, with an option to animate the deletion.

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

Parameters of [UITableView deleteRowsAtIndexPaths withRowAnimation]
indexPaths
An array of NSIndexPath objects identifying the rows to delete.
animation
A constant that indicates how the deletion is to be animated, for example, fade out or slide out from the bottom. See “Table Cell Insertion and Deletion Animation” for descriptions of these constants.

Discussion of [UITableView deleteRowsAtIndexPaths withRowAnimation]
Note the behavior of this method when it is called in an animation block defined by the beginUpdates and endUpdates methods. UITableView defers any insertions of rows or sections until after it has handled the deletions of rows or sections. This happens regardless of ordering of the insertion and deletion method calls. This is unlike inserting or removing an item in a mutable array, where the operation can affect the array index used for the successive insertion or removal operation. For more on this subject, see “Batch Insertion and Deletion of Rows and Sections” in Table View Programming Guide for iOS.

UITableView deleteRowsAtIndexPaths withRowAnimation example.
Normally in order to animate UITableView's cell animation, we have to do something like this:

[tableView_ beginUpdates];
[tableView_ deleteRowsAtIndexPaths: indexPathDelete withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView_ insertRowsAtIndexPaths: indexPathInsert withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView_ reloadRowsAtIndexPaths: indexPathReload withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView_ endUpdates];

Example of [UITableView deleteRowsAtIndexPaths withRowAnimation].
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //Get the object to delete from the array.
        identifyObject = [appDelegate.idArray objectAtIndex:indexPath.row];
        [appDelegate removeID:identifyObject]; // update model first

        // now you can check model count and do what you want
        if ([appDelegate.idArray count] == 0) // I think you mean appDelegate.idArray
        {  
            // do what you want
            // with  [self.idTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }

        else
        {
            [self.idTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
}

UITableView deleteRowsAtIndexPaths withRowAnimation example.
-(void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

//make array of the objects and remove the selected object from that array.
NSMutableArray *delArray = [faves objectForKey:[entries objectAtIndex:indexPath.section]];
[delArray removeObjectAtIndex:indexPath.row];

//write the updated entries to the plist file.
NSArray *rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [rootPath objectAtIndex:0];
NSString *plistFile = [docsPath stringByAppendingPathComponent:@"Data.plist"];
[faves writeToFile:plistFile atomically:YES];

//update the actual tableview. This is where things go awry.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; //ends up trying to load the sections instead of the entries = wrong number of list items returned = crash!

//[tableView reloadData]; //works if I only use this line, but the list updates wrong, showing each entry as a section header.
}

End of UITableView deleteRowsAtIndexPaths withRowAnimation example article.