在我努力学习如何将数据正确加载到Core Data的分组表视图中时,我构建了一个非常简单的小型双视图应用程序。我使用MagicalRecord作为我与CD的接口。
当前的问题是,当输入数据按预期保存到我的商店时,表格视图在保存时不会刷新以显示新数据。但是,如果我在模拟器中停止应用程序,然后从Xcode重新启动,新数据将显示在tableview中。
我正在使用NotificationCenter而不是委托进行通信。这是我最初的View Controller(包含和委托给tableview)的代码,后面是AddViewController中用来添加数据的代码。有魔法记忆经验的人能否对我有所启发?谢谢!
//
// ViewController.m
// MRGroupingTest
//
// Created by Tim Jones on 1/9/14.
// Copyright (c) 2014 TDJ. All rights reserved.
//
#import "ViewController.h"
#import "ListActivity.h"
@interface ViewController ()
{
NSFetchedResultsController *frc;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self refreshData];
frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationNewActivityAdded:) name:@"newActivityAdded" object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) notificationNewActivityAdded:(NSNotification*)notification
{
[self.myTableView reloadData];
}
#pragma mark Table View data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[frc sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id<NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:section];
int rows = [sectionInfo numberOfObjects];
return rows;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:section];
return [sectionInfo name];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
if ([frc sections] > 0)
{
ListActivity *activity = [frc objectAtIndexPath:indexPath];
cell.textLabel.text = activity.name;
}
return cell;
}
-(void) refreshData
{
frc = [ListActivity MR_fetchAllGroupedBy:@"category" withPredicate:nil sortedBy:@"name" ascending:YES];
[self.myTableView reloadData];
}
#pragma mark Table View delegate
@end
这是来自AddViewController的代码:
//
// AddViewController.m
// MRGroupingTest
//
// Created by Tim Jones on 1/9/14.
// Copyright (c) 2014 TDJ. All rights reserved.
//
#import "AddViewController.h"
@interface AddViewController ()
@end
@implementation AddViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveAction:(UIBarButtonItem *)sender
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
ListActivity * thisActivity = [ListActivity MR_createInContext:localContext];
thisActivity.name = self.activityField.text;
thisActivity.category = self.categoryField.text;
[localContext MR_saveToPersistentStoreAndWait];
//Inform app
[[NSNotificationCenter defaultCenter] postNotificationName:@"newActivityAdded" object:localContext];
//dismiss view
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)cancelAction:(UIBarButtonItem *)sender
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[[NSNotificationCenter defaultCenter] postNotificationName:@"newActivityAdded" object:localContext];
//dismiss view
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
@end
答案 0 :(得分:0)
您需要重新加载TableView数据,即调用reloadData
函数