Friday, June 7, 2013

UITableView endUpdates example in Objective C (iOS).


UITableView endUpdates

Conclude a series of method calls that insert, delete, select, or reload rows and sections of the receiver.

- (void)endUpdates

Discussion of [UITableView endUpdates]
You call this method to bracket a series of method calls that began with beginUpdates and that consist of operations to insert, delete, select, and reload rows and sections of the table view. When you call endUpdates, UITableView animates the operations simultaneously. Invocations of beginUpdates and endUpdates can be nested. If you do not make the insertion, deletion, and selection calls inside this block, table attributes such as row count might become invalid.

UITableView endUpdates example.
- (void)tableView:(UITableView *)tableView performOperation:(void(^)())operation completion:(void(^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.0 animations:^{

        [tableView beginUpdates];
        if (operation)
            operation();
        [tableView endUpdates];

    } completion:^(BOOL finished) {

        if (completion)
            completion(finished);
    }];
}

Example of [UITableView endUpdates].
- (void)setDownloadedImage:(NSMutableDictionary *)d {
    NSIndexPath *indexPath = (NSIndexPath *)[d objectForKey:@"IndexPath"];
    [indexPathDelayed addObject:indexPath];
    if (!([table isDragging] || [table isDecelerating])) {
        [table beginUpdates];
        [table insertRowsAtIndexPaths:indexPathDelayed withRowAnimation:UITableViewRowAnimationFade];
        [table endUpdates];
        // --> Call Method Here <--
        loadingView.hidden = YES;
        [indexPathDelayed removeAllObjects];
    }
}

UITableView endUpdates example.
[CATransaction begin];

[CATransaction setCompletionBlock:^{
    // animation has finished
}];

[tableView beginUpdates];
// do some work
[tableView endUpdates];

[CATransaction commit];

End of UITableView endUpdates example article.