2013-03-28 61 views
0

我最近正在研究一個應用程序,它允許用戶將項目添加到順序列表中,並且他們可以按下按鈕來調出popover表視圖,並且我正在使用核心數據來存儲數據。UITableView不重新載入數據

該應用程序有一個主表視圖,每行都會顯示另一個表視圖,就像具有滑動菜單的Facebook應用程序一樣。

應用的佈局如下所示:在發生以下情形

|---------------------------------------------------------------| 
|            ______________ | 
|            |Popover Button|| 
|            -------------- | 
|---------------------------------------------------------------| 
|-----------------------------|First table item 1    | 
|Row to call the First table |         | 
|-----------------------------|---------------------------------| 
|Row to call the Second table |First table item 2    | 
|-----------------------------|         | 
|Row to call the Third table |---------------------------------| 
|-----------------------------|First table item 3    | 
|Row to call the Fourth table |         | 
|-----------------------------|---------------------------------| 

問題:

  1. 啓動的應用程序。

  2. 選擇第一個表格視圖。

  3. 按下添加按鈕將一些項目添加到列表中。

  4. 按下彈出式窗口按鈕查看彈出式窗口視圖(列表已更新)。

  5. 關閉彈出式表格視圖。

  6. 進一步添加一些更多的項目到列表中。

  7. 按下彈出按鈕(列表保持不變,表格視圖尚未更新)。

  8. 轉到另一個表視圖(或再次調用同一個表視圖)再次

  9. 按酥料餅的按鈕(列表然後被更新)。

誰能幫我整理一下嗎?謝謝!

下面是代碼:

OrderView.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.navigationItem.leftBarButtonItem = self.editButtonItem; 

    //load lists 
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Dish"]; 
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES]]; 

    self.lists = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil]; 

    [self.tableView reloadData]; 
} 

- (NSManagedObjectContext *)managedObjectContext 
{ 
    return [(AppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext]; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete the row from the data source 

     [self.managedObjectContext deleteObject:[self.lists objectAtIndex:indexPath.row]]; 
     [self.managedObjectContext save:nil]; 

     NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Dish"]; 
     fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES]]; 

     self.lists = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil]; 

     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 

DetailView.m

- (IBAction)popupAddToOrderButtonPressed:(id)sender 
{ 
    OrderViewController *orderView = [[OrderViewController alloc] initWithNibName:@"OrderViewController" bundle:nil]; 
    Dish *newList = [NSEntityDescription insertNewObjectForEntityForName:@"Dish" inManagedObjectContext:orderView.managedObjectContext]; 
    newList.created = [NSDate date]; 
    newList.title = self.popupTitle.text; 
    [orderView.managedObjectContext save:nil]; 
    orderView.lists = [orderView.lists arrayByAddingObject:newList]; 
} 
+1

我還沒有調試上面的代碼,以確定爲什麼表不顯示新的數據,但你這樣做是困難的。查看「NSFetchedResultsController」的文檔。它被設計成用於顯示來自核心數據存儲區的數據的「UITableView」的數據源,並且將使該實現變得更簡單。 – XJones

回答

0

我使用NSNotification來解決這個問題,因爲該NSNotification將通過整個應用程序,所以我在popover表視圖中設置了NSNotification接收器,並且當popover表視圖接收到通知時,它將執行獲取請求以獲取數據。

當按下添加按鈕時,它會發出通知,要求彈出視圖來獲取數據。

就這樣,問題解決了。