2011-04-06 64 views
1

我有ViewController(A)與TableView,其中每行是一個目錄名稱。每行都推送一個新的ViewController(B)。在ViewController B裏面我正在運行刪除當前打開的目錄的方法。但是,當我彈出視圖並返回到TableView時,我顯然仍然有這個目錄在列表中。我該如何解決它?iPhone - 從更高的ViewController刷新TableView VC

如何在導航返回時刷新TableView?

不,簡單[self.tableView reloadData]裏面-viewWillAppear並不真正的工作。

代碼:

我-viewWillAppear:

- (void)viewWillAppear:(BOOL)animated { 
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    self.arrFolders = [[NSMutableArray alloc] init]; 
    [self allFilesInDocuments:docDir]; 
} 

-allFilesInDocuments只是將項目添加到陣列([arrFolders addObject:folder];)。

裏面-tableView:cellForRowAtIndexPath:

cell.textLabel.text = [arrFolders objectAtIndex:indexPath.row]; 
+0

[self.tableView reloadData];其簡單的方法來刷新表格。 – 2011-04-06 13:57:53

+0

你甚至讀過整個問題嗎? – yosh 2011-04-06 14:04:49

回答

4

使用NSNotification。

在ViewController中(A)的viewDidLoad中註冊的通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deletedRow:) name:@"deletedRow" object:nil]; 

另外添加選擇器功能:

-(void) deleteRow:(NSNotification *)notif{ 
[self.tableView reloadData]; 

}

最後,在子視圖控制器,在您的刪除功能發佈通知:

[[NSNotificationCenter defautCenter] postNotificationName:@"deletedRow" object:nil userInfo:nil]; 

希望它有幫助

+0

甜蜜,工作真的很好,謝謝:) – yosh 2011-04-06 14:11:35

0

一個編輯數據添加到數據源和-viewWillAppear重載應的工作雖然沒有這個,你可以通過一個父視圖控制器調用或NSNotification執行更新。

+0

NSNotification更好,在沒有任何東西被刪除時爲您節省不必要的重新加載。 – 2011-04-06 14:09:05

+0

爲項目視圖製作一個自定義委託並將其分配給父項,這也可以起作用,涉及更多的代碼,但通常更清晰。 – DavidM 2011-04-06 14:39:51