Ossir
Ossir

Reputation: 3145

How to leave UITableViewCellEditingStyleDelete mode when user edit it with swipe gesture

I need to leave delete mode after user tapped Delete button. I want to show some activity indicator and wait the server response on delete action before I actually remove the cell (or not if the server does not respond). This action I want to perform from delegate method:

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

How can I do it?

Upvotes: 0

Views: 2829

Answers (3)

Paulo
Paulo

Reputation: 1245

Hi Let me get it straight: Your datasource is actually online and you want confirmation that it is deleted before you update your tableview - in the mean time you want to display an AI Activity indicator.

If that is what you want to do then you start with this method

  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
 // Get the key of the data you want to delete.

 data_struct_instance * data =  [self.mutableArray_data_source objectForRowAtIndexPath:indexPath];
 NSString *data_key = data.key;
 row_index = indexPath.row;  //   set a property to preserve the row (or index path for multilevel data) for use when you delete the the record later.

 [self start_UIActivityIndicator];
 [self callonline_with_delete_command:data_key]; //  send the request to delete the record

  }

once the server responds depending on whether it is successful or not you can either delete the record or reload the entire array if the table is small this is preferable to ensure that the data is synced -

 ....  
    [activityindicator StopAnimating];

   if (success) {

     [self.mutableArray_data_source removeObjectAtIndex:row_index];}
  else {
       NSLog .... 
         }

   [self.tableView reloadData];  // resets the delete button. 

Upvotes: 0

monK_
monK_

Reputation: 397

To hide the "Delete"-Button, you need to use the setEditing:animated: method.

However, your implementation of tableView:commitEditingStyle:forRowAtIndexPath: needs to execute this with a slight delay. The note in the reference Table View Programming Guide for iOS underneath Figure 7-1 states:

Note: The data source should not call setEditing:animated: from within its implementation of tableView:commitEditingStyle:forRowAtIndexPath:. If for some reason it must, it should invoke it after a delay by using the performSelector:withObject:afterDelay: method.

So your implementation could be like this:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // remove delete button only after short delay
        [self performSelector:@selector(hideDeleteButton:) withObject:nil afterDelay:0.1];
    }
}

- (void)hideDeleteButton:(id)obj
{
    [self.tableView setEditing:NO animated:YES];
}

The above code makes the Delete-Button slide away after 0.1 seconds when the user presses it. You will then, of course, need to prevent it going back into the editing mode while waiting for the action to complete. For that, you can override the UITableViewDataSource method tableView:canEditRowAtIndexPath: to prevent the same cell or the whole table to be edited again while waiting.

Upvotes: 4

Jeef
Jeef

Reputation: 27275

In my code I make a call to

[self.tableView reloadData];

I'm not sure if this is the 100% correct way to do it but it works for me. So your function could be something like:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do your processing.

[self.tableView reloadData];

}

It should then clear out the red Delete Button and refresh your data.

You may also be able to play around with the call

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

but in my experience its the reloadData call that seems to do the trick

Upvotes: 1

Related Questions