在我的项目中,我有一个用于以类似于地址簿应用程序的方式添加/显示/编辑联系人的机制。我使用核心数据进行数据管理。基本上,当应用程序加载时,我所有的当前联系人都在那里,但是当我添加一个新联系人并返回到所有联系人的表视图时,我看不到更改。然后,当我关闭应用程序并再次打开它时,可以看到更改。有人可以让我知道我做错了什么吗?我想在添加新联系人后刷新它。当视图出现时在表格中加载新数据
allathletes.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAthlete)];
self.navigationItem.rightBarButtonItem = addButton;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
_managedObjectContext = [appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *athlete = [NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext];
[request setEntity:athlete];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"first" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil){
//handle error
}
[self setAthleteArray:mutableFetchResults];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)addAthlete{
AthleteAdd *addAthlete= [self.storyboard instantiateViewControllerWithIdentifier:@"addAthlete"];
[self.navigationController pushViewController:addAthlete animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Athlete Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Athlete *athlete = (Athlete *)[athleteArray objectAtIndex:indexPath.row];
cell.textLabel.text = [athlete first];
cell.detailTextLabel.text = [athlete last];
// Configure the cell...
return cell;
}
addathlete.m
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
_managedObjectContext = [appDelegate managedObjectContext];
UIBarButtonItem *saveButton=[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(saveAthlete)];
UIBarButtonItem *cancelButton=[[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:@selector(cancelSave)];
self.navigationItem.rightBarButtonItem = saveButton;
self.navigationItem.leftBarButtonItem = cancelButton;
}
-(void)saveAthlete{
Athlete *athlete = [NSEntityDescription insertNewObjectForEntityForName:@"Athlete" inManagedObjectContext:_managedObjectContext];
[athlete setFirst:firstTextField.text];
[athlete setLast:lastTextField.text];
NSError *error = nil;
if(![_managedObjectContext save:&error]){
//handle dat error
}
[[self navigationController] popViewControllerAnimated:YES];
}
-(void)cancelSave{
[[self navigationController] popViewControllerAnimated:YES];
}
同样的问题在这里。 http://stackoverflow.com/questions/17940662/uitableviewcontroller-not-updating – luca590
尝试用新的数据源重新加载表。 –