coredata错误使tableView无法显示(NSFetchResultsController)
CoreData: error: Serious application error. An exception was caught from the delegate of
NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of sections.
The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (0), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null)
像这样的错误?我怎么知道?我的代码出了什么问题..我不明白这个错误..任何人都可以告诉我那是什么?我该如何解决这个问题呢?
节数始终为 1.. 为什么更新前从 0 开始?如何让表在更新之前识别出有 1 个且只有 1 个部分?
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.FetchController sections] count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.FetchController sections] objectAtIndex:section];
/*int numberOfRow=[sectionInfo numberOfObjects]-(self.numberOf20SkipFromtheBeginning*20);
numberOfRow = MIN(20,numberOfRow);*/
//int numberOfRow=MIN(100,[sectionInfo numberOfObjects]);
CalledRowCountingNotYetCallRowForSection=true;
[self.tableViewA setBounces:YES];
if([sectionInfo numberOfObjects]>100){
//[Timer searchCriteriaChanged];
CLog(@"Something Wrong This NumberOfRow: %d", [sectionInfo numberOfObjects]);
}
return [sectionInfo numberOfObjects];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DLog(@"Function TableView AT listnewController");
CalledRowCountingNotYetCallRowForSection =false;
if ([self tableView:tableView numberOfRowsInSection:indexPath.section]==0)
{
DLog(@"Something is VERY WRONG. Number of Section: %d" ,indexPath.section);
}
DLog(@"Indexpath at cellForRowAtIndexPath : %d", indexPath);
Business * theBiz=[self.FetchController objectAtIndexPath:indexPath];
return theBiz.CustomCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DLog(@"Function didselectrowatindexpath listnewcontroller");
//[BNUtilitiesQuick UtilitiesQuick].currentBusiness=[[self.FetchController fetchedObjects] objectAtIndex:[indexPath row]+self.numberOf20SkipFromtheBeginning*20];
[BNUtilitiesQuick UtilitiesQuick].currentBusiness=[[self.FetchController fetchedObjects] objectAtIndex:[indexPath row]];
[BNUtilitiesQuick SafelyPushControllerAtNavigationController:[BNUtilitiesQuick NearbyIsiKota].mDetailBusiness atNavigationController:[BNUtilitiesQuick NearbyIsiKota].navigationController];
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
DLog(@"controllerWillChangeContent: %@", controller);
[self.tableViewA beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
DLog(@"didChangeSection: %@", controller);
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableViewA insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableViewA deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
DLog(@"controller:%@\ndidChangeObject:%@\natIndexPath:%@\nforChangeType:%d\nnewIndexPath:%@\n",controller, anObject, indexPath, type, newIndexPath);
UITableView *tableView = self.tableViewA;
DLog(@"%@", self.tableViewA);
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeUpdate:
//[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationRight];
//[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationRight];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableViewA endUpdates];
}
CoreData: error: Serious application error. An exception was caught from the delegate of
NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of sections.
The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (0), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null)
the error like that? how can I know? what's wrong with my code.. I don't understand with that error.. anyone can tell me what is that? and how can I fix that hell?
number of section is always 1.. Why it start of 0 before the update? How do I make the table recoqnize that there is 1 and only 1 section before any updating?
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.FetchController sections] count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.FetchController sections] objectAtIndex:section];
/*int numberOfRow=[sectionInfo numberOfObjects]-(self.numberOf20SkipFromtheBeginning*20);
numberOfRow = MIN(20,numberOfRow);*/
//int numberOfRow=MIN(100,[sectionInfo numberOfObjects]);
CalledRowCountingNotYetCallRowForSection=true;
[self.tableViewA setBounces:YES];
if([sectionInfo numberOfObjects]>100){
//[Timer searchCriteriaChanged];
CLog(@"Something Wrong This NumberOfRow: %d", [sectionInfo numberOfObjects]);
}
return [sectionInfo numberOfObjects];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DLog(@"Function TableView AT listnewController");
CalledRowCountingNotYetCallRowForSection =false;
if ([self tableView:tableView numberOfRowsInSection:indexPath.section]==0)
{
DLog(@"Something is VERY WRONG. Number of Section: %d" ,indexPath.section);
}
DLog(@"Indexpath at cellForRowAtIndexPath : %d", indexPath);
Business * theBiz=[self.FetchController objectAtIndexPath:indexPath];
return theBiz.CustomCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DLog(@"Function didselectrowatindexpath listnewcontroller");
//[BNUtilitiesQuick UtilitiesQuick].currentBusiness=[[self.FetchController fetchedObjects] objectAtIndex:[indexPath row]+self.numberOf20SkipFromtheBeginning*20];
[BNUtilitiesQuick UtilitiesQuick].currentBusiness=[[self.FetchController fetchedObjects] objectAtIndex:[indexPath row]];
[BNUtilitiesQuick SafelyPushControllerAtNavigationController:[BNUtilitiesQuick NearbyIsiKota].mDetailBusiness atNavigationController:[BNUtilitiesQuick NearbyIsiKota].navigationController];
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
DLog(@"controllerWillChangeContent: %@", controller);
[self.tableViewA beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
DLog(@"didChangeSection: %@", controller);
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableViewA insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableViewA deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
DLog(@"controller:%@\ndidChangeObject:%@\natIndexPath:%@\nforChangeType:%d\nnewIndexPath:%@\n",controller, anObject, indexPath, type, newIndexPath);
UITableView *tableView = self.tableViewA;
DLog(@"%@", self.tableViewA);
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeUpdate:
//[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationRight];
//[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationRight];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableViewA endUpdates];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能是由于在 NSFetchedResultsController 不需要时为 NSFetchedResultsController 提供了一个
sectionNameKeyPath
造成的。如果您提供
sectionNameKeyPath
但该键路径还没有值,则获取的结果控制器将报告该表有零个部分。如果该键路径获取一个值,则获取的结果控制器将报告该表现在有一个或多个部分。要修复此问题,请确保在初始化获取的结果控制器时
sectionNameKeyPath
为空。您还可以硬连线numberOfSectionsInTableView:
以始终返回零或一。This is most likely caused by providing a
sectionNameKeyPath
for the NSFetchedResultsController when it doesn't need one.If you provide a
sectionNameKeyPath
but there are no values yet for that key path, the fetched results controller will report that the table has zero sections. If that key path acquires a value, the fetched result controller will report that the table now has one or more sections.To fix, make sure that
sectionNameKeyPath
is empty when you initialize the fetched results controller. You can also hardwirenumberOfSectionsInTableView:
to always return zero or one.