手势识别器滑动以从核心数据中删除

时间:2014-04-10 03:26:06

标签: ios objective-c uitableview core-data uigesturerecognizer

我正在处理使用核心数据的应用程序,我想添加一个手势,如果用户向左滑动它将删除数据库中的对象。我已经设置了手势识别器部件,我在创建从tableview和核心数据中删除对象的方法时遇到了麻烦。因此,如果有人能够看到这一点,并指出我正确的方向,我将不胜感激!谢谢!

TableView.M

-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
        NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
        NSManagedObjectContext *context = [self managedObjectContext];

            // Delete object from database
            [context deleteObject:[self.lists objectAtIndex:indexPath.row]];

            // Remove list from table view
            [self.lists removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        }

@end

错误:

self(ViewController *)  0x8ea0a90   
indexPath=(NSIndexPath *    )  nil  
context+(NSManagedObjectContext *)  0x8d747f0   

1 个答案:

答案 0 :(得分:0)

试试这个

-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
    NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
    NSManagedObjectContext *context = [self managedObjectContext];

        // Delete object from database
        [context deleteObject:[self.lists objectAtIndex:indexPath.row]];
        [context save:nil];

        // Remove list from table view
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.lists removeObjectAtIndex:indexPath.row];
}