从表视图中删除数据会导致应用程序崩溃

时间:2016-08-10 06:24:13

标签: ios objective-c uitableview core-data

这里当我点击删除按钮时,应用程序崩溃给出错误:尝试从对象[0]中插入nil对象 这是我的代码。

@implementation ViewDetailViewController

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

- (void)viewDidLoad {
    [super viewDidLoad];


    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Personal"];
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    self.searchResult = [NSMutableArray arrayWithArray:self.devices];

    [self.tableView reloadData];


    // Do any additional setup after loading the view, typically from a nib.
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Personal"];
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    self.searchResult = [NSMutableArray arrayWithArray:self.devices];

    [self.tableView reloadData];


    // Do any additional setup after loading the view.
}

这是我的表视图委托方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.searchResult count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    NSManagedObject *device = [self.searchResult objectAtIndex:indexPath.row];

    {
        [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"name"]]];
        [cell.detailTextLabel setText:[device valueForKey:@"attribute"]];
    }
    return cell;
}

删除数据的代码。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObjectContext *context = [self managedObjectContext];

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete object from database
        [context deleteObject:[self.devices objectAtIndex:indexPath.row]];

        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        }
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];

        //Remove device from table view
        [self.devices removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

3 个答案:

答案 0 :(得分:1)

在数据源方法中,您使用self.searchResult

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.searchResult count];
}

但在commitEditingStyle:中,您从[self.devices removeObjectAtIndex:indexPath.row];

中删除了数据

你需要改变这个

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [self.devices count];
    }

或更改

[self.searchResult removeObjectAtIndex:indexPath.row];

还有一件事你不需要cellIndexPath这个参数传递给方法

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSManagedObjectContext *context = [self managedObjectContext];

 if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete object from database
            [context deleteObject:[self.devices objectAtIndex:indexPath.row]];

            NSError *error = nil;
            if (![context save:&error]) {
                NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
                return;
            }

        //Remove device from table view
        [self.devices removeObjectAtIndex:indexPath.row];
        //[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }
}

答案 1 :(得分:0)

请将此代码用完

[self.searchResult removeObjectAtIndex:indexPath.row];
[tableview reload];

答案 2 :(得分:0)

尝试删除此行:[tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationFade];  看来你不应该在commitEditingStyle中删除单元格。委托自动执行它。