iOS在我的tableview中撤消已删除的单元格

时间:2016-03-11 06:09:29

标签: ios objective-c uitableview

在我的项目中,我只是想在滑动时隐藏单元格。如果我从tableview隐藏单元格撤消按钮将生成我的导航栏顶部。单击撤消按钮,隐藏的单元格应重新出现。

这是我的代码..

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {


    Download *download = [self.downloadManager.downloads objectAtIndex:indexPath.row];

    // Get path to documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsPath = [paths objectAtIndex:0];

    // Get zip file from path..

    NSString *fileInDocumentsPath = [documentsPath stringByAppendingPathComponent:download.name];

    NSLog(@"Looking for zip file %@", fileInDocumentsPath);

    if ([[NSFileManager defaultManager] fileExistsAtPath:fileInDocumentsPath])
    {
        UITableViewRowAction *HideAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Hide" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
            //insert your editAction here

            [self.downloadManager.downloads removeObjectAtIndex:indexPath.row];

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

            [tableView endUpdates];

            UIBarButtonItem *UndoButton = [[UIBarButtonItem alloc] initWithTitle:@"Undo" style:UIBarButtonItemStyleBordered target:self action:@selector(UndoAction:)];
            self.navigationItem.rightBarButtonItem=UndoButton;

        }];
        HideAction.backgroundColor = [UIColor blueColor];

        UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Delete"  handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
            //insert your deleteAction here



        }];
        deleteAction.backgroundColor = [UIColor redColor];
        return @[deleteAction,HideAction];

    }
    else
    {

        UITableViewRowAction *HideAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Hide" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
            //insert your editAction here
        }];
        HideAction.backgroundColor = [UIColor blueColor];
        return @[HideAction];

    }



}

1 个答案:

答案 0 :(得分:1)

我认为您必须在撤消堆栈中使用NSUndoManager进行记录操作,您可以轻松撤消。

link将帮助您获得更多想法。