How to get out of UITableViewCellEditingStyleDelete mode when user is editing it with gestures

I need to leave the delete mode after clicking the Delete user button. I want to show an activity indicator and wait for the server to respond to the delete action before I delete the cell (or not if the server is not responding). This is the action I want to take from the delegate method:

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

      

How can i do this?

0


source to share


3 answers


To hide the Delete button, you need to use the method setEditing:animated:

.

However, your implementation tableView:commitEditingStyle:forRowAtIndexPath:

should take a slight delay in doing this. Note in the iOS Spreadsheet Programming Guide under Figure 7-1:

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



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 slider slide pressed after 0.1 seconds when the user clicks on it. You will, of course, have to prevent it from returning to edit mode while waiting for the action to complete. To do this, you can override the UITableViewDataSource method tableView:canEditRowAtIndexPath:

to prevent re-editing the same cell or the entire table while waiting.

+4


source


In my code I am calling

[self.tableView reloadData];

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

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

[self.tableView reloadData];

}

      



Then you should clear the red Delete button and update your data.

You can also play with a challenge

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

but in my experience its a challenge reloadData

that seems to do the trick

+1


source


Hey. Let me understand: your datasource is actually online and you want it to be deleted before updating the table. This means you want to display the AI ​​activity indicator.

If this is what you want to do 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

  }

      

after the server response, depending on whether it succeeds or not, you can either delete the entry or reload the entire array, if the table is small, this is preferable to keep the data in sync -

 ....  
    [activityindicator StopAnimating];

   if (success) {

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

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

      

0


source







All Articles