From fade50eef5298ccf62b725c5363b33141b4cb9a6 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Sat, 12 Sep 2020 01:25:56 -0700 Subject: [PATCH] Adds swipe action to delete a card from the library. --- MonsterCards/Views/LibraryViewController.m | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/MonsterCards/Views/LibraryViewController.m b/MonsterCards/Views/LibraryViewController.m index e3b28fa..b392348 100644 --- a/MonsterCards/Views/LibraryViewController.m +++ b/MonsterCards/Views/LibraryViewController.m @@ -73,4 +73,20 @@ return cell; } +#pragma mark - UITableViewDelegate + +- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView +trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { + UIContextualAction *action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:NSLocalizedString(@"Delete", @"Command to delete an object.") handler:^(UIContextualAction *action, __kindof UIView *sourceView, void (^completionHandler)(BOOL actionPerformed)) { + Monster *monster = [self.allMonsters objectAtIndex:indexPath.row]; + [self->_context deleteObject:monster]; + [self->_context save:nil]; + self.allMonsters = [self->_context executeFetchRequest:[Monster fetchRequest] error:nil]; + [self.tableView reloadData]; + }]; + + UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:[NSArray arrayWithObject:action]]; + return config; +} + @end