Iphone 了解NSFetchedResultsController的工作原理
首先,如果你有时间帮助我,非常感谢 我一直在学习Iphone 了解NSFetchedResultsController的工作原理,iphone,ios,Iphone,Ios,首先,如果你有时间帮助我,非常感谢 我一直在学习nsfetchedresultscoontroller,我试图尽可能多地理解它,但是,关于它的一些事情让我非常困惑。这段代码来自我在网上找到的教程。基本上,我有一个最简单的抓取控制器,它是用像这样非常奇特的方法设置的 - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { // The fetch controller is about t
nsfetchedresultscoontroller
,我试图尽可能多地理解它,但是,关于它的一些事情让我非常困惑。这段代码来自我在网上找到的教程。基本上,我有一个最简单的抓取控制器,它是用像这样非常奇特的方法设置的
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
(NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"FailedBankInfo" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"details.closeDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:_context sectionNameKeyPath:nil
cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[sort release];
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
NSFetchedResultsController
。我有一个barbutton add项和一个名为AddButton pressed
的方法,当按下该方法时,会添加一个视图(从下至上)。我用同样的方法
Entity myEntity=[NSEntityDescription insertNNewObjectForEntityForName:@“Entity”inManagedObjectContext:context_]代码>然后我告诉代码进入另一个视图但是,当动画向上移动新导航控制器时,新单元格已在动画中显示。从我的理解这两种方法
-(void)configureCell:(UITableViewCell*)单元格atIndexPath:(NSIndexPath*)indexPath
和-(UITableViewCell*)tableView:(UITableView*)tableView
与上面代码段中的前两个方法一起被调用。我怎样才能修好它?我不想在当前视图上显示任何内容,除非我通过转到另一个视图添加了新实体。另一个对象被调用到上下文中的第二个对象,必须调用update方法或其他方法
4我在哪里可以了解更多有关这方面的信息。i、 e单个内容更新方法的作用以及调用它们的时间等。NSFetchedResultcontroller是否也充当单个数组?这意味着,如果我想要一个包含多个部分的tableview,我需要更多的ViewController?再次感谢 我赞同布拉德的评论,但我会给你一些建议:
[NSEntityDescription insertNNewObjectForEntityForName:@“Entity”
从tableViewController调用,并且只要调用NsfeetedResultsController,就会收到一条消息,说明插入了一行,从而触发控制器:didChangeObject:atIndexPath:forChangeType:NewIndexXPath:
方法中的NsfeetedResultsChangeInsert路径。NSFetchedResultsController是一个对象,它监视从CoreData获取的数据是否有任何更改,然后让您能够在更改发生时执行操作。要解决显示问题,只需将insertNewObjectForEntityName调用移动到下一个viewController中,当您关闭该viewController时,就会有一个新行我建议您阅读苹果的文档,可能还有他们的。不要忘记下载并使用示例代码,在大多数情况下,它比文档中的理论更容易理解。我建议将此问题细化为一个问题,并将其他问题作为单独的问题提问。现在这里有太多的问题要回答。
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}