Friday, June 7, 2013

UITableView UITableViewRowAnimationAutomatic example in Objective C (iOS).


UITableView UITableViewRowAnimationAutomatic

Table Cell Insertion and Deletion Animation
The type of animation when rows are inserted or deleted.

typedef enum {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
} UITableViewRowAnimation;

Constants
UITableViewRowAnimationFade
The inserted or deleted row or rows fades into or out of the table view.
UITableViewRowAnimationRight
The inserted row or rows slides in from the right; the deleted row or rows slides out to the right.
UITableViewRowAnimationLeft
The inserted row or rows slides in from the left; the deleted row or rows slides out to the left.
UITableViewRowAnimationTop
The inserted row or rows slides in from the top; the deleted row or rows slides out toward the top.
UITableViewRowAnimationBottom
The inserted row or rows slides in from the bottom; the deleted row or rows slides out toward the bottom.
UITableViewRowAnimationNone
No animation is performed. The new cell value appears as if the cell had just been reloaded.
UITableViewRowAnimationMiddle
The table view attempts to keep the old and new cells centered in the space they did or will occupy. Available in iPhone 3.2.
UITableViewRowAnimationAutomatic
The table view chooses an appropriate animation style for you. (Introduced in iOS 5.0.)

Discussion of [UITableView UITableViewRowAnimationAutomatic]
You specify one of these constants as a parameter of the insertRowsAtIndexPaths:withRowAnimation:, insertSections:withRowAnimation:, deleteRowsAtIndexPaths:withRowAnimation:,deleteSections:withRowAnimation:, reloadRowsAtIndexPaths:withRowAnimation:, and reloadSections:withRowAnimation: methods.

UITableView UITableViewRowAnimationAutomatic example.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:control.tag-100 inSection:0];
[resultView beginUpdates];
[resultList removeObjectAtIndex:indexPath.row];
[resultView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[resultView endUpdates];

Example of [UITableView UITableViewRowAnimationAutomatic].
int sectionNumber = 0; //Note that your section may be different

int nextIndex = [currentItems count]; //starting index of newly added items

[myTableView beginUpdates];

for (NSObject *item in itemsToAdd) {
    //Add the item to the data source
    [currentItems addObject:item];

    //Add the item to the table view
    NSIndexPath *path = [NSIndexPath indexPathForRow:nextIndex++ inSection:sectionNumber];
    [myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationAutomatic];
}

[myTableView endUpdates];

UITableView UITableViewRowAnimationAutomatic example.
int indexOfNewSection = 4; // TODO: change to meaningful value
[self.tableview beginUpdates];
[self.tableview insertSections:[NSIndexSet indexSetWithIndex:indexOfNewSection]
              withRowAnimation:UITableViewRowAnimationAutomatic];

// Update data model
[self.sections insertObject:sectionObj atIndex:indexOfNewSection];

[self.tableview endUpdates];

End of UITableView UITableViewRowAnimationAutomatic example article.