我正在使用NSFetchedResultsController。以前我有一个类似的问题,当数据库没有用于tableview的条目但创建了一个时,我发现必须至少有一个部分,所以我解决了这个问题。但现在它崩溃了,例如我有两个部分,每个部分都有一行,我删除了一行,所以部分应该消失 - >崩溃。它表示更新前的部分数量(2)不等于删除的数量(0)。使用NSFetchedResultsController删除节中的最后一行 - >崩溃,使用NSFetchedResultsController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return 1 if the fetchedResultsController section count is zero
return [[fetchedResultsController sections] count] ? : 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// check if we really have any sections in the managed object:
if (!fetchedResultsController.sections.count) return @"Persoonlijk";
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// check if we really have any sections in the managed object:
if (!fetchedResultsController.sections.count) return 0;
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
更新 方法,其中行被删除:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete schedule
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1);
}
}
}
这是怎么回事:return [[fetchedResultsController sections] count]? :1;你错过了中间的参数。我想你实际上错过了第一个,并让他们处于错误的位置。 – Jim 2012-03-06 21:50:44
它看起来很奇怪,但实际上是正确的:)这是一个简短的手返回版本[[fetchedResultsController部分] count]> 0? [[fetchedResultsController sections] count]:1; – Pieter 2012-03-06 21:58:19
我发现这条消息通常意味着在实际获得消息进行删除/删除的方法中出现了问题。从你的文章看来,你实际上并没有改变它的部分数量?也许发布您的部分删除代码? – AlexTheMighty 2012-03-06 22:06:05