How can I tell When a UITableView animation has finished? - iphone

How can I tell when [UITableView setEditing:YES animated:YES] has completed?
I don't want to give any context, because I want to avoid anybody giving me workarounds, which do not interest me.
What I want is to call the above, then have a separate function called when the animation is completed.
I am edited the post to give context and some workarounds.
Originally I setEditing and immediately reload the table data.
[tableView setEditing:YES animated:YES];
[tableView reloadData];
The problem is that the table reloads before the animation begins, and so the animation is never seen.
Here are some various workarounds:
[tableView setEditing:YES animated:YES];
[self performSelector:#selector(ReloadTable) withObject:nil afterDelay:1.0];
This works but if I get the delay incorrect then it will look bad. So I need to know what the delay is, which I can figure out, but we are not gauranteed that the delay will always be the same.
isEditing = YES;
[tableView reloadData];
[tableView setEditing:YES animated:YES];
This could work, but the table behaves differently depending on if we are in editing mode. So I have to use my own isEditing variable instead of the standard UITableView.editing. I would rather not have to create a new boolean isEditing variable.
[tableView setEditing:YES animated:YES];
[tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationTop];
This almost works well but in editing mode the first row should have the UITableViewCellEditingStyleInsert, while the other rows get UITableViewCellEditingStyleDelete. And with the above code the editing style gets set BEFORE the row is added. Therefore the second row ends up with UITableViewCellEditingStyleInsert.

[CATransaction begin];
[CATransaction setCompletionBlock: ^{
// your animation has finished
}];
[tableView setEditing:YES animated:YES];
[CATransaction commit];
Note that setCompletionBlock must be on the top.

In iOS 4 you can do the following:
[UIView animateWithDuration:0.3f
animations:^{
[self.tableView setEditing:YES animated:NO];
}
completion:^(BOOL finished){
// Do something
}
];

Swift 4 version of accepted answer:
CATransaction.begin()
CATransaction.setCompletionBlock {
// your animation has finished
}
tableView.setEditing(true, animated: true)
CATransaction.commit()

Related

reload UITableViewCell only after Insert-Animation finished

so i'm inserting a UITableViewCell into my tableView.
[searchTableView beginUpdates];
[searchTableView insertRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[searchTableView endUpdates];
after this i'm running an asynchronous request which updates the tableviewcell with a fade animation.
[someRequestWithCompletion:^(id data) {
dispatch_async(dispatch_get_main_queue(), ^{
[searchTableView beginUpdates];
[searchTableView reloadRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[searchTableView endUpdates];
});
}];
but it is possible that this request finishes before the insert animation is finished. if the request finishes after the insert-animation, there is no problem, if it finishes before and calls a reloadRowsAtIndexPaths on the same cell that is currently inserting, the cell displays immediately and forces a reload while the insert-animation fades out.
is there a way to trigger the reload update after the cell is completely inserted? what would be the best way to do this?
if there are any questions, please let me know in the comments
Try This..
U can reload your TableView After Completion of Animation.
[CATransaction begin];
[searchTableView beginUpdates];
[CATransaction setCompletionBlock: ^{
[searchTableView reloadData];
}];
[searchTableView deleteRowsAtIndexPaths: [NSArray arrayWithObjects: indexPath, nil]
withRowAnimation: UITableViewRowAnimationAutomatic];
[searchTableView endUpdates];
[CATransaction commit];

Call a method after [self.tableView endUpdates] ended

I want to add my sliderButton after the animation has ended but this doesn't work, it adds the button while the deletion of the section is being animated.
[coursesTable beginUpdates];
[coursesTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
[coursesTable endUpdates];
[self.view addSubview:sliderButton];
Is there a possibility I can call a method that does that for me when the animation ended?
I believe that wrapping your table update in animateWithDuration would work:
[UIView animateWithDuration:0.0 animations:^{
[coursesTable beginUpdates];
…
[coursesTable endUpdates];
} completion:^(BOOL finished) {
// Code to run when table updates are complete.
}];
Other methods that I found suggested here on Stack Overflow did not work for me.
I used this technique at one time and tested it enough to verify that the completion block was called after I called the table's endUpdates method, but rewrote my code so I didn't need it any more before I had completely verified that the animation was actually finished.
You can find the answer here:
How to detect that animation has ended on UITableView beginUpdates/endUpdates?
Hope it helps!
I have also not found the solution of this.I have to use performSelector:withObject:afterDelay: for getting my work done.
[coursesTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
after this the tableview again asks data from its datasource... so all the datasource methods including cellforRow is again called..
i suggest keeping a BOOL is Deleting.
so now do this
[coursesTable beginUpdates];
[coursesTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
[coursesTable endUpdates];
Deleting = YES;
in cellForRow method
{
if (index path.row == lastrow ) // last rowcell is reached
if(Deleting)
{
//tableView has reloaded.
[self.view addSubview:sliderButton];
Deleting = NO;;
}
}

animation issues when editing a uitableview

This is really simple, though still driving my nuts. I have a uitableview where I am trying to animate transition in and out of editing mode. This is what I took from an example that I have seen. It does do the job, but without the animation.
Any thoughts?
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:YES];
[tblSimpleTable setEditing:NO animated:YES];
[tblSimpleTable reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[tblSimpleTable setEditing:YES animated:YES];
[tblSimpleTable reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
PS: I am also not sure why I need this line: [super setEditing:NO animated:YES]; but things just dont seem to work at all without it. I just saw a few examples online that dont do that.
Thanks!
Maybe you should not reloadData when set editing property.
BTW, What's your "super" class? Normally you don't have to invoke [super setEditing:YES animated:YES];
Is it only the button that isn't animating properly? Either way you should probably be using super.editButtonItem instead of your own; it's animated and just setting the text and style like that (I believe) isn't. As far as calling the super, are you overriding one of the editing methods and not calling the super method from within there? And xuzhes's answer about the reloadData is, I believe, correct as well.
Try this:
#Implementation YourViewController // This can (should) be a subclass of UITableViewController to make your life easier
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem; // Automatically calls setEditing:animated: and changes itself to "Edit"/"Done" between presses
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing == YES) {
// Do stuff here
} else {
// Do stuff here
}
// Reload all sections of the table view
// THIS IS THE PART YOU'RE INTERESTED IN
NSRange range = NSMakeRange(0,[self.tableView numberOfSections]);
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}
Check out the documentation for comments on the methods from Apple :)

UITableView editing not working

I have a button (a UINavigationBarItem) used for editing my UITableView, which only allows deletions. So when I press delete, the little red line comes up next to each cell, and I can delete each row.
When the button is pressed, the following function is called:
-(void)editButtonSelected:(id)sender {
if(self.editing)
NSLog(#"self.editing = true");
else
NSLog(#"self.editing = false");
if(self.editing) {
[super setEditing:NO animated:NO];
[tableView setEditing:NO animated:NO];
[tableView reloadData];
[leftButton setTitle:#"Delete"];
[leftButton setStyle:UIBarButtonItemStylePlain];
self.editing = false;
}
else {
[super setEditing:YES animated:YES];
[tableView setEditing:YES animated:YES];
[tableView reloadData];
[leftButton setTitle:#"Done"];
[leftButton setStyle:UIBarButtonItemStyleDone];
self.editing = true;
}
}
And it works fine. But only for a while. As soon as I introduce a new UIViewController, and then dismiss that controller, this delete function doesn't work on this main screen I have. It works fine until a new UIViewController is put on top. The button itself works fine, and the value of self.editing does get toggled between true and false correctly, but the little red lines do not show up. Why could this be happening?
I would guess that the target on your UINavigationBarItem is still set to the first view controller, not the subsequent view controllers that get pushed on the stack.

UITableView Animation when entering Editmode

Maybe I´m just stupid but I cant understand why this isnt working.
I want to achieve a little animation when I'm entering editing mode within a UITableView.
[super setEditing:NO animated:YES];
[myTable setEditing:NO animated:YES];
[myTable reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
Shouldnt this animated:YES suppose to animated this entering of the editmode?
Regards.
- f0rz
Solution, I was reloading the tableview . This was making the animation to stop.
Removed [myTable reloadData] and it worked again!