0

我有一个 UITableView,其中有从 UIWebView 下载的文件。在我进行更改之前,我可以毫无问题地删除该行以及从它下载到的本地文件夹中。

从那时起,我实现了多选功能。按编辑,选择 w/e 文件,按删除以显示操作表工作正常。但是对于我的生活,我无法弄清楚如何使操作表处理删除操作。

下面我将发布我正在使用的代码。

//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);
            }
        }
    }
}

任何有关如何链接“确定”按钮以完成删除的信息将不胜感激。

4

2 回答 2

1

您必须实现 UIActionSheet 委托方法

actionSheet:didDismissWithButtonIndex:

将完美地工作

编辑

在 UIActionSheet 委托方法中添加setEditing:YES只会将 tableView 置于编辑模式,不会提交删除。我不知道您如何检索要删除的行,但编辑模式只允许单行选择。

我认为对于您希望完成的事情可能更好的是操纵您的didSelectRowAtIndexPath方法来标记每一行以进行删除,并将选定的索引添加到数组中,或者用复选标记标记行。

然后在UIActionSheet用户点击“确定”时的委托方法中,放置删除逻辑,使用索引数组或遍历单元格以找到您选择删除的单元格。

我会尝试这样做,因为正如您所经历的那样,自定义删除按钮和commitEditingStyle协议方法很难链接,因为如何调用此方法。

于 2013-07-23T15:35:30.203 回答
0
 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
      if (buttonIndex == actionSheet.destructiveButtonIndex) 
      {
              //Delete
              [yourCell setEditing:YES animated:YES];

      }
}
于 2013-07-23T15:41:56.943 回答