0

我有几个UIViewControllers,显示我的'用户'对象的表视图作为部分和'网站'对象作为每个部分下的行。网站与用户的关系是一对多的关系。NSFetchResultController委托错误

目前我显示这个数据用下面的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0]; 
    return [sectionInfo numberOfObjects]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    User *sectionUser = (User*)[self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:section inSection:0]]; 

    return [[sectionUser.sitesToUser allObjects] count]; 
} 

在我NSFetchResultsController我只是拉着所有用户对象,没什么特别的。

现在我的问题 - 每当我更新的结果,通过移除部分(即用户)或添加一个用户,而这一观点也打开了,我得到一个NSFetchedResultsController委托错误:

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 (2) must be equal to the number of sections contained in the 
table view before the update (1), plus or minus the number of sections inserted or 
deleted (0 inserted, 0 deleted). with userInfo (null) 

我只在我以这种方式显示数据的视图中获取它,是用户是部分,站点在每个部分下是行。

我做的事情真的很愚蠢吗?我认为它会删除该部分,当我删除它等...似乎工作正常,如果每个用户是在UITableView的行 - 我认为这是我的问题的症结...

+0

“网站与用户的关系是一对多的。”它似乎相反。 – an0

回答

0

我正在使用核心数据,但我对numberOfSectionsInTableView:tableView:numberOfRowsInSection:方法代码是不同的...我不知道这是否可以解决你的问题,但是,尝试使用此代码:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [[self.fetchedResultsController sections] count]; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSInteger rowsNumber = 0; 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
    rowsNumber = [sectionInfo numberOfObjects]; 
    return rowsNumber; 
} 

对于部分,我从来没有用过但是在初始化控制器时试图在initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:的参数sectionNameKeyPath上工作。

0

您还没有表现出你的fetchedResultsController委托调用:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
- (void)controller:didChangeObject:atIndexPath:forChangeType:newIndexPath 
- (void)controller:didChangeSection:sectionInfoAtIndex:forChangeType: 

这些调用负责添加和删除的行/从你的桌子部分,所以一切“增加了”尽可能的实现代码如下关注。

在你的情况下,你有点“滥用”某个部分是什么,所以这段代码可能会变得棘手。

如果你愿意没有动画做的,你可以只实现:

-(void)controllerDidChangeContent:(NSFetchedResultsController*) controller { 
    [self.tableView reloadData]; 
} 

丹尼尔。