2011-09-30 81 views
0

我的表视图是从包含核心数据中的数据的数组填充的,我想在删除行的同时更新核心数据,这里是我的删除代码。删除与核心数据关联的表视图中的行

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

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source. 
     NSManagedObject *selectedObject = [arrayList objectAtIndex:[indexPath row]]; 
     [managedObjectContext deleteObject:selectedObject]; 

     [arrayList removeObjectAtIndex:[indexPath row]]; 

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

不过,我收到以下错误,当我点击删除按钮:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995 *** 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 (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

我的数据源是arrayList含NSManagedObjects从核心数据获取。

想法?提前致谢!


更新:

现在可以删除工作,我删除后的ArrayList中的数据,但在核心数据的数据没有得到相应的删除,当应用程序重新登场,名单仍然是相同的。

回答

0

这意味着你删除的行后,也将数据源仍然返回4的所以现在在第0行数只已显示了第3行的表认为,应该有4

从您的描述中,您可能需要从arrayList中删除对象,并从CoreData中删除它。或者从CoreData删除对象后重新加载arrayList,然后删除该行。

+0

谢谢〜现在删除可以正常工作,但核心数据中的相应数据也不会被删除。我更新了我的代码。 – Michael

+0

我解决了我的问题,我忘了保存上下文.... shamed〜 – Michael