samvermette
samvermette

Reputation: 40437

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?

Upvotes: 9

Views: 5024

Answers (3)

Duck
Duck

Reputation: 35953

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.
}];

Upvotes: 2

Rudolf Adamkovič
Rudolf Adamkovič

Reputation: 31486

What about this?

[CATransaction begin];

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

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

[CATransaction commit];

Upvotes: 24

XJones
XJones

Reputation: 21967

Sorry to say, I don't think you can do this. When you make changes to the table in after beginUpdates has been called the changes are animated as a single animation after endUpdates. There are no animation callbacks during these animations. I haven't tried this so don't know if it would work well for this but you could try nesting beginUpdates and endUpdates and updating your shadow after each endUpdates.

Upvotes: 0

Related Questions