CoreData:在返回到 UITableView 之前重新加载数据
CoreData: Reload Data Before returning to UITableView
我知道核心数据与 UITableViewController 密切相关,但是我有一个应用程序有两个页面,您可以在其中更新数据,如果我可以的话就太好了查看反映在每个页面的相关标签中的更新值,而无需 return 到 UITableViewController 并使用 tableView.reloadData()。有没有办法做到这一点?
谢谢
编辑:我添加了一张图表,希望它能让我的问题更清楚一些。我想更新视图 D 中的托管对象,我可以确认(通过 println)已经按预期工作,但是当我 poptoViewController C 时,没有任何改变,我看到的是过时的信息.只有当我 return 到表视图 A 时,对象的更改才会在 UI 中变得明显。那么有没有一种方法可以在视图 C 上重新加载对象而不必返回到 A?
编辑 #2:阅读下面的回复后(谢谢!),我认为我的问题归结为 如何在 ViewController 上实现 NSFetchedResultsController 的功能有一个 UITableView?
NSFetchedResultsController
几乎免费为您提供此功能。实施 NSFetchedResultsControllerDelegate
并设置 frc.delegate = self
。当在核心数据中进行更改时,将调用适当的委托方法
下面的代码是 iOS Master/Detail 核心数据模板的样板,Xcode 为您设置了该模板。要获取所有这些的副本,请转到文件 -> 新建 -> 项目 -> 主从应用程序 -> 然后检查 "Use Core Data"。
Swift
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case .Insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
return
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Update:
self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
case .Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
default:
return
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
Objective-C
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
default:
return;
}
}
- (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
关于你的问题:
How can I implement the functionality of NSFetchedResultsController on
a ViewController that does not have a UITableView?
您的 ViewController 应该订阅 NSManagedObjectContextDidSaveNotification
通知。查看通知的 userInfo 并处理键 updatedObjects
、insertedObjects
和 deletedObjects
。
当您对任何对象感兴趣时 ViewController 然后更新视图。
这里is an example
我知道核心数据与 UITableViewController 密切相关,但是我有一个应用程序有两个页面,您可以在其中更新数据,如果我可以的话就太好了查看反映在每个页面的相关标签中的更新值,而无需 return 到 UITableViewController 并使用 tableView.reloadData()。有没有办法做到这一点? 谢谢
编辑:我添加了一张图表,希望它能让我的问题更清楚一些。我想更新视图 D 中的托管对象,我可以确认(通过 println)已经按预期工作,但是当我 poptoViewController C 时,没有任何改变,我看到的是过时的信息.只有当我 return 到表视图 A 时,对象的更改才会在 UI 中变得明显。那么有没有一种方法可以在视图 C 上重新加载对象而不必返回到 A?
编辑 #2:阅读下面的回复后(谢谢!),我认为我的问题归结为 如何在 ViewController 上实现 NSFetchedResultsController 的功能有一个 UITableView?
NSFetchedResultsController
几乎免费为您提供此功能。实施 NSFetchedResultsControllerDelegate
并设置 frc.delegate = self
。当在核心数据中进行更改时,将调用适当的委托方法
下面的代码是 iOS Master/Detail 核心数据模板的样板,Xcode 为您设置了该模板。要获取所有这些的副本,请转到文件 -> 新建 -> 项目 -> 主从应用程序 -> 然后检查 "Use Core Data"。
Swift
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case .Insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
return
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Update:
self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
case .Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
default:
return
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
Objective-C
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
default:
return;
}
}
- (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
关于你的问题:
How can I implement the functionality of NSFetchedResultsController on a ViewController that does not have a UITableView?
您的 ViewController 应该订阅 NSManagedObjectContextDidSaveNotification
通知。查看通知的 userInfo 并处理键 updatedObjects
、insertedObjects
和 deletedObjects
。
当您对任何对象感兴趣时 ViewController 然后更新视图。
这里is an example