2013-08-01 36 views
1

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

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]; 

} 
+0

同樣的問題在這裏。 http://stackoverflow.com/questions/17940662/uitableviewcontroller-not-updating – luca590

+0

嘗試用新的數據源重新加載表。 –

回答

1

新的運動員被添加到持久性存儲,但你的表沒有顯示你更新的數據。添加新的運動員對象後,從持久性存儲獲取更新的數據到AthleteArray(步驟1)並重新載入您的表格視圖(步驟2)。

步驟1:

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]; 

步驟2:

[self.tableView reloadData]; 
+0

謝謝Prazad! –

+0

歡迎。快樂編碼:) – Prazad

1

重載tabelViewviewDidAppear: OR viewWillAppear:方法。在popdismiss時,viewDidLoad:方法不會呼叫。這就是爲什麼更新在tableView中不可見的原因。

相關問題