当通过UINavigation添加按钮添加实体时,当前在我的应用程序中重新加载UITableView时出现问题,该按钮触发视图控制器实现文件中的addHero方法。从核心数据的角度来看,添加和删除实体的效果非常好,因为当退出并重新启动应用程序时,所有实体都会在tableview中正确显示。当选择标签栏时,视图重新加载时,它也可以正常工作。我已经尝试重新加载表视图,但我不确定我需要在序列中调用此方法。
#pragma mark- NSFetchedResultsControllerDelegate Methods
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.heroTableView beginUpdates];
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[_heroTableView reloadData];
[self.heroTableView endUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch (type) {
case NSFetchedResultsChangeInsert:
[self.heroTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.heroTableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
switch (type) {
case NSFetchedResultsChangeInsert:
[self.heroTableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.heroTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:
case NSFetchedResultsChangeUpdate:
break;
}
}
#pragma mark- UIAlertViewDelegateMethods
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
exit(-1);
}
- (IBAction)addHero:(id)sender {
NSManagedObjectContext *managedObjectContext = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest]entity];
[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:managedObjectContext];
NSError *error =nil;
BOOL sucess =[managedObjectContext save:&error];
if (!sucess) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Error saving entity", @"Error Saving entity") message:[NSString stringWithFormat:NSLocalizedString(@"Error was: %@, quitting.", @"Error was: %@,quitting." )] delegate:self cancelButtonTitle:NSLocalizedString(@"Aw Nuts", @"Aw Nuts") otherButtonTitles: nil];
[alert show]
}
}
答案 0 :(得分:1)
这些行存在冲突:
[_heroTableView reloadData];
[self.heroTableView endUpdates];
您可以在beginUpdates
和endUpdates
之间更新表格,也可以放弃动画,只需致电reloadData
即可。以上有不可预知的结果。
如果您的代理人工作正常,则应在必要时插入行,您应不致电reloadData
。从它的外观来看,你正在使用样板NSFetchedResultsControllerDelegate
,所以它应该按预期工作。
答案 1 :(得分:0)
终于想出了这个。我忘记将获取的结果控制器的委托设置为tableviewcontroller。但是我最初在viewDidLoad方法中做了这个,这导致了问题。最好的解决方案是在getter方法的实例化时设置fetchedResultscontoller的委托。
_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sectionKey cacheName:@"Hero"];
_fetchedResultsController.delegate=self;
return _fetchedResultsController;