Friday, June 7, 2013

UITableView UITableViewRowAnimationRight example in Objective C (iOS).


UITableView UITableViewRowAnimationRight

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 UITableViewRowAnimationRight]
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 UITableViewRowAnimationRight example.
if (segmentControl.selectedSegmentIndex == 0)
{
    self.settings.useMetric = YES;
    if ([sections containsObject:FT_AND_IN] && [sections containsObject:FRACTION_PRECISION]) {

        NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]];
        [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]];

        [sections removeObject:FT_AND_IN];
        [sections removeObject:FRACTION_PRECISION];

        [self.tableView deleteSections:indexSections
             withRowAnimation:UITableViewRowAnimationRight];
     }
 }
 else {
     self.settings.useMetric = NO;
     [sections insertObject:FT_AND_IN atIndex:1];
     [sections insertObject:FRACTION_PRECISION atIndex:2];

     NSMutableIndexSet *indexSections = [NSMutableIndexSet indexSetWithIndex:[sections indexOfObject:FT_AND_IN]];
     [indexSections addIndex:[sections indexOfObject:FRACTION_PRECISION]];

     [self.tableView insertSections:indexSections
          withRowAnimation:UITableViewRowAnimationRight];
}

Example of [UITableView UITableViewRowAnimationRight].
NSMutableArray *indexes = [NSMutableArray arrayWithCapacity:5];

for (NSDictionary *entry in [dl reverseObjectEnumerator])
{
    int insertIdx = 0;

    [_allEntries insertObject:entry atIndex:insertIdx];
    [indexes addObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]];
}

// insert rows into table
[self.tableView insertRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationRight];

UITableView UITableViewRowAnimationRight example.
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];

End of UITableView UITableViewRowAnimationRight example article.