Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run other code after table view delete row animation is completed

I am using the standard table view datasource protocol to delete table cells:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

I would like to run some other code once the animation is completed but the deleteRowsAtIndexPaths:withRowAnimation method does not have a completion block. How else would I run some code after this method completes?

like image 559
denvdancsk Avatar asked Aug 27 '13 21:08

denvdancsk


2 Answers

For iOS 6 Apple added - tableView:didEndDisplayingCell:forRowAtIndexPath: to UITableViewDelegate. You should be able to use it to get a callback immediately when each UITableViewCell has been removed from the display, so if you know you've started a deletion animation on a particular index path then you can use it as a usually reliable means of knowing that the animation has ended.

(aside: I guess if the user scrolled the cell off screen during its animation then you could get a false positive, but such things are going to be so unlikely that I'd be likely to add a basic guard against persistent negative consequences and not worry about the ephemeral ones, such as if I end up showing an empty cell when the mid-deletion object is scrolled back onto screen because I already removed it from my store)

like image 181
Tommy Avatar answered Oct 21 '22 18:10

Tommy


I believe one way to do this is to implement UITableViewDataSource's method tableView:commitEditingStyle:forRowAtIndexPath: and execute a delayed performance method there inside.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (UITableViewCellEditingStyleDelete == editingStyle) {

        [self performSelector:@selector(delayedMethod) withObject:nil afterDelay:0.1];
    }
}

-(void)delayedMehtod {
    // Your code here...
}

It may not be as pretty as a "completion" block but I'm sure it would do the trick.

Hope this helps!

like image 31
LuisCien Avatar answered Oct 21 '22 17:10

LuisCien