在我的項目中,我有一個用於以類似於地址簿應用程序的方式添加/顯示/編輯聯繫人的機制。我使用核心數據進行數據管理。基本上,當應用程序加載時,我所有的當前聯繫人都在那裏,但是當我添加一個新聯繫人並返回到所有聯繫人的表視圖時,我看不到更改。然後,當我關閉應用程序並再次打開它時,可以看到更改。有人可以讓我知道我做錯了什麼嗎?我想在添加新聯繫人後刷新它。當視圖出現時在表格中加載新數據
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
嘗試用新的數據源重新加載表。 –