Friday, June 7, 2013

UITableView reloadRowsAtIndexPaths withRowAnimation example in Objective C (iOS).

UITableView reloadRowsAtIndexPaths withRowAnimation

Reloads the specified rows using a certain animation effect.

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

Parameters of [UITableView reloadRowsAtIndexPaths withRowAnimation]
An array of NSIndexPath objects identifying the rows to reload.
A constant that indicates how the reloading 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.
The animation constant affects the direction in which both the old and the new rows slide. For example, if the animation constant is UITableViewRowAnimationRight, the old rows slide out to the right and the new cells slide in from the right.

Discussion of [UITableView reloadRowsAtIndexPaths withRowAnimation]
Reloading a row causes the table view to ask its data source for a new cell for that row. The table animates that new cell in as it animates the old row out. Call this method if you want to alert the user that the value of a cell is changing. If, however, notifying the user is not important—that is, you just want to change the value that a cell is displaying—you can get the cell for a particular row and set its new value.[UITableView reloadRowsAtIndexPaths withRowAnimation]

When this method is called in an animation block defined by the beginUpdates and endUpdates methods, it behaves similarly to deleteRowsAtIndexPaths:withRowAnimation:. The indexes that UITableView passes to the method are specified in the state of the table view prior to any updates. This happens regardless of ordering of the insertion, deletion, and reloading method calls within the animation block.

UITableView reloadRowsAtIndexPaths withRowAnimation example.
[self.tableView beginUpdates];
NSArray *reloadIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:section]];
[self.tableView reloadRowsAtIndexPaths:reloadIndexPath withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView endUpdates];

Example of [UITableView reloadRowsAtIndexPaths withRowAnimation].
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    NSIndexPath *previousSelectedIndexPath = selectedIndexPath;  // <- save previously selected cell
    selectedIndexPath = indexPath;
    if (previousSelectedIndexPath) { // <- reload previously selected cell (if not nil)
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousSelectedIndexPath]
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectedIndexPath]

UITableView reloadRowsAtIndexPaths withRowAnimation example.
i update the row using:

  [self.tableView beginUpdates];
  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:answerRowPath]
  [self.tableView endUpdates];

on iOS 5, there is no hidden cell

End of UITableView reloadRowsAtIndexPaths withRowAnimation example article.