如何在添加后立即从UITableView中删除一行?

时间:2012-01-17 20:09:09

标签: xcode templates core-data

我已在UITableView添加了以下方法,为用户提供删除行的选项:

(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

我已使用选项

从XCode模板复制此方法
  

使用CoreData进行存储

用户按下addButton中的UITableView后,会打开一个新的UIView,并在其中输入新实体的信息。

然后,用户触摸saveButton中的UIView。读取信息并将其存储在上下文中。 UIView已关闭,用户将返回UITableView

新条目现在显示在新行中。如果用户现在意识到他犯了一个错误并且想立即删除该行(从左到右擦除然后触摸“删除”)该行仍然存在并且没有其他任何事情发生。我抛出以下错误:

An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. 
Invalid update: invalid number of rows in section 0. 
The number of rows contained in an existing section after the update (1)
must be equal to the number of rows contained in that section before the update (1), 
plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted). 
with userInfo (null)

但是如果我在删除之前退出应用程序,则删除行会有效。 (并重新查看,然后删除)

仅在controllerDidChangecontent中调用[self.tableView endUpdate]

我的方法如下:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the managed object for the given index path
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

        //NSLog(@"DATE: %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);


        for (UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {

            NSLog(@"N: %@", notification.fireDate);
            NSLog(@"O: %@", [[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:@"zeit"]);

            if (notification.fireDate == [[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:@"zeit"]) {
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
                NSLog(@"GELÖSCHT!");
            }
        }

        // Save the context.
        NSError *error = nil;
        if (![context save:&error]) {
            /*
            Replace this implementation with code to handle the error appropriately.

            abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
            */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }   
}

我认为该行已添加但未提交。然后它会尝试删除它不知道的行。我能正确理解吗? 但我不确切知道在哪里修复错误。 我能做什么?有人知道吗?

1 个答案:

答案 0 :(得分:2)

查看错误消息:您还必须从表格视图中删除该行

如果您使用的是NSFetchedResultsController,则可以这样做:

// in controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
case NSFetchedResultsChangeDelete:
   [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                    withRowAnimation:UITableViewRowAnimationFade];
break;

另请注意,必须通知您fetchedResultsController应重新获取新的结果集 - 否则tableView:numberOfRowsInSection将返回旧值。所以你需要插入:

self.fetchedResultsController = nil;

它将使用正确的部分和行懒洋洋地重新创建。