Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView beginUpdates/endUpdates callback

I'm making changes to a tableView using a beginUpdates/endUpdates block. Throughout the way I need to update a drop shadow so that it reflects the tableView's current composition.

I tried setting up KVO for the tableView's contentSize but it's only called on endUpdates, once the animation has finished. What I want is for it to be called every time contentSize changes (even if it's by only a pixel). Is there any way to achieve this?

like image 775
samvermette Avatar asked Jan 09 '12 20:01

samvermette


2 Answers

What about this?

[CATransaction begin];

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

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

[CATransaction commit];
like image 88
Rudolf Adamkovič Avatar answered Oct 12 '22 23:10

Rudolf Adamkovič


Rudolf's method did not work for me as smooth as expected. In my case I was selecting a row on UITableView using this, and Rudolf's method was causing the table to do two animations with a little freeze: the animation inside beginUpdates/endUpdates, a little freeze and the animation on the completion block.

 [tableView selectRowAtIndexPath:indexPath
                        animated:YES
                  scrollPosition:scrollPosition];

that inspired me to create this code... and this is working seamlessly:

[UIView animateWithDuration:0.0 animations:^{
    [tableView beginUpdates];
    // do something to the table
    [tableView endUpdates];
} completion:^(BOOL finished) {
    // Code to run when table updates are complete.
}];
like image 38
Duck Avatar answered Oct 13 '22 00:10

Duck