ChrisOSX
ChrisOSX

Reputation: 744

Action sheet UIButton to process delete

I have a UITableView in which I have files downloaded to from a UIWebView. Before my changes I had no issues deleting the row and from the local folder in which it was downloaded to.

Since then I have implemented a multi select function. Pressing edit, selecting w/e files, pressing delete to show the action sheet works fine. But for the life of me I can't figure out how to make the action sheet process the delete action.

Below I will post the code I'm working with.

//viewDidLoad:
self.deleteButton = [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(deleteButton:)];

- (void)deleteButton:(id)sender
{
    NSString *actionTitle = ([[self.tableView indexPathsForSelectedRows] count] == 1) ?
    @"Are you sure you want to remove this item?" : @"Are you sure you want to remove these items?";
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:actionTitle delegate:self cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:@"OK" otherButtonTitles:nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];

}

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

    if (editingStyle == UITableViewCellEditingStyleDelete){

        NSString *fileName = [directoryContents objectAtIndex:indexPath.row];

        NSString *path;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"downloads"];
        path = [path stringByAppendingPathComponent:fileName];
        NSError *error;

    //Remove cell
        [directoryContents removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        //[tableView reloadData];

        if ([[NSFileManager defaultManager] fileExistsAtPath:path])     //Does file exist?
        {
            if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error])   //Delete it
            {
                NSLog(@"Delete file error: %@", error);
            }
        }
    }
}

Any info on how to link that "OK" button to finish the delete would be greatly appreciated.

Upvotes: 0

Views: 758

Answers (2)

Chris Tetreault
Chris Tetreault

Reputation: 1973

You have to implement the UIActionSheet Delegate methods

actionSheet:didDismissWithButtonIndex:

will work perfectly

EDIT

Adding setEditing:YES inside the UIActionSheet delegate method will only place the tableView into editing mode, not commit the deletion. I don't know how you are retrieving the rows you wish to delete, but Editing mode only allows a single row selection.

I think what may be better for what you wish to accomplish is manipulate your didSelectRowAtIndexPath method to mark each row for deletion, and either add the selected indices to an array, or mark the rows with a checkmark.

Then in the UIActionSheet delegate method when the users hits 'OK', place the deletion logic, either use the indices array or iterate through the cells to find the ones you selected for deletion.

I would try this because, as you have experienced, a custom delete button and the commitEditingStyle protocol method are difficult to link up because of how this method is invoked.

Upvotes: 1

Abdullah Shafique
Abdullah Shafique

Reputation: 6918

 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
      if (buttonIndex == actionSheet.destructiveButtonIndex) 
      {
              //Delete
              [yourCell setEditing:YES animated:YES];

      }
}

Upvotes: 0

Related Questions