在UITABLEVIEWCONTROLLER中删除记录时出错?

时间:2013-09-25 21:05:35

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

我想从tableviewcontroller单元格中删除记录。

这是我的ViewController.h文件的代码

#import <UIKit/UIKit.h>

@interface IMSCategoryViewController : UITableViewController
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property(nonatomic,retain)NSArray *arr;
@property (readonly, strong, nonatomic) NSMutableArray *categoryArray;
@end

这个在实现文件中。

#import "IMSCategoryViewController.h"
#import "IMSAppDelegate.h"
#import "Category.h"

@interface IMSCategoryViewController ()
{
    NSManagedObjectContext *context;
}
@end

@implementation IMSCategoryViewController
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize categoryArray;
@synthesize arr;

我正在执行删除操作。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSManagedObject *recordtoDelete = [arr objectAtIndex:indexPath.row];

        [_managedObjectContext deletedObject:recordtoDelete];



        [arr objectAtIndex:indexPath.row];

       [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

        NSError *error = nil;
        if (![_managedObjectContext save:&error]) {
            //handle error here
        }

    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

OKAY所以当我运行这段代码时......

它给出了这种类型的错误......

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:'

Invalid update: invalid number of rows in section 0.
The number of rows contained in an existing section after the update (15) must be equal
to the number of rows contained in that section before the update (15), plus or minus
the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus 
or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

1 个答案:

答案 0 :(得分:0)

您需要deleteObject:方法,而不是返回NSSet的deletedObjects属性。