1

I have ViewController (A) with TableView where each row is a directory name. Each row is pushing a new ViewController (B). Inside ViewController B I am running method to delete the currently opened directory. However, when I pop the view and go back to TableView I obviously still have this directory on list. How can I fix it?

How can I refresh the TableView while going back in navigation?

And no, simple [self.tableView reloadData] inside -viewWillAppear doesn't really work.

Code:

My -viewWillAppear:

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

-allFilesInDocuments is just adding items to array ([arrFolders addObject:folder];).

Inside -tableView:cellForRowAtIndexPath: I add data with

cell.textLabel.text = [arrFolders objectAtIndex:indexPath.row];
yosh
  • 3,245
  • 7
  • 55
  • 84

2 Answers2

4

Use a NSNotification.

In the viewDidLoad of ViewController (A) register for a notification:

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

Also add the selector function:

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

}

Finally in the child view controllers, in your Delete function post the notification:

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

Hope it helps

Stefan Ticu
  • 2,093
  • 1
  • 12
  • 21
0

An edit to the datasource and a reload in -viewWillAppear should of worked though failing this you could perform the update via an parent view controller call or an NSNotification.

DavidM
  • 548
  • 1
  • 4
  • 11
  • NSNotification is better, saves you the unnecessary reloads when nothing was deleted. – Stefan Ticu Apr 06 '11 at 14:09
  • Making a custom delegate for the item view and assigning it to the parent, that would work as well, bit more code involved but generally cleaner. – DavidM Apr 06 '11 at 14:39