Friday, June 7, 2013

UITableView UITableViewRowAnimationFade example in Objective C (iOS).


UITableView UITableViewRowAnimationFade

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 UITableViewRowAnimationFade]
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 UITableViewRowAnimationFade example.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [db DeleteData:[NSString stringWithFormat:@"%d",indexPath.row]];
        [data removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];

    }  

}

Example of [UITableView UITableViewRowAnimationFade].
- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];  // possibly not necessary depending upon your class hierarchy
    [tvController.tableView setEditing:editing animated:animated];
    [UIView animateWithDuration:0.25 delay:0.05 options:UIViewAnimationCurveLinear
                     animations:^{
                         [tvController.tableView beginUpdates];

                         if (editing == YES) {
                             [tvController.tableView deleteRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:UITableViewRowAnimationFade];
                         } else {
                             UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
                             [tvController.tableView reloadRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:animation];
                             [tvController.tableView reloadSectionIndexTitles];
                             self.navigationItem.hidesBackButton = editing;
                         }
                         [tvController.tableView endUpdates];
                     }
                     completion:nil];
}

UITableView UITableViewRowAnimationFade example.
- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{

    [tvController.tableView setEditing:editing animated:animated]; //this LOC is added

    [tvController.tableView beginUpdates];

    if (editing == YES) {
        [tvController.tableView deleteRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:UITableViewRowAnimationFade];

}    else {

        UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
        [tvController.tableView reloadRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:animation];

        [tvController.tableView reloadSectionIndexTitles];

        self.navigationItem.hidesBackButton = editing;
    }
        [tvController.tableView endUpdates];
}

-(void)deleteRecord:(NSInteger)recordNo:(NSInteger)sectionNo:(BOOL)isEditMode:(BOOL)isAnimate {

if(isEditMode){
    NSIndexPath *indexP=[NSIndexPath indexPathForRow:recordNo inSection:sectionNo];
    [tvController.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationLeft];
}
else {

    if(isAnimate)
        [tvController.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationFade];
    else
        [tvController.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationNone];

    [tvController.tableView reloadSectionIndexTitles];
    self.navigationItem.hidesBackButton = editing;

}

[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(reload) userInfo:nil repeats:NO];
}

 -(void)reload {
    [table reloadData];
}

End of UITableView UITableViewRowAnimationFade example article.