ioscore-datatableviewmodal-view

iOS TableView Reload after dismissing modal


In my app, I am listing core data entries in a tableview. I want to allow the user to edit records using a detail view presented modally as a form view. I am observing peculiar behavior when editing records.

The flow:

  1. User loads tableview with records. -working
  2. User selects a record for editing. -working
  3. User edits record in a view controller presented as a modal form view. -working
  4. User saves edits and dismisses form view. -working
  5. Tableview shows correct changes for the previously edited record. -working
  6. User repeats steps 2 - 4 selecting a different record to edit. -working
  7. Tableview shows correct data for all records. -Not Working.

At step 7, the tableview reverts the display of the first edited record to its original state. Subsequent record edits result in all previous edits reverting to their original state. If the tableview is dismissed and reloaded, the records are correct, showing all the edits.

I have used [tableview reload] in the tableview's ViewWillAppear method, but it does not seem to be fired when the modal form view controller is dismissed.

In my tableviewcontroller code:

-(void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
}

Searching around, I have not found a solution and am hoping someone can point me in the right direction.

Thanks!


Solution

  • When you presenting a modal view, main view controller's view never disappears. So, after you dismiss your modal view, viewWillAppear() won't be called.

    You could try to implement a custom delegate function in your modal view, and set it in the main view controller, when your data is updated, fire the delegate function to reload your tableView, which is at your main viewController.

    Understand what is delegate function in iOS, and

    how to create a delegate function is like this:

    1. In your ModalView.h:

      // define the protocol for the delegate
      @protocol ModelViewDelegate <NSObject> 
        - (void) didUpdateData;
      @end
      
      @interface ModalView: ViewController {
           //create a delegate instance
           id delegate;
      }
      
      // define delegate instance
      @property (nonatomic, assign) id <ModelViewDelegate> delegate;
      
    2. In your modalView.m:

      @synthesize delegate;
      

      and then inside your function, put the delegate function at place where you need to fire, for example:

      - (void) updateDataIntoDatabase{
             ....
             //Update work done.
      
             [self.delegate didUpdateData];
             //dismiss your modalView;
      }
      

    So, in your MainViewController.h,

    #import ModalView.h
    

    and

    @interface ModalView: ViewController <ModelViewDelegate > {
          ...
    }
    

    Inside your MainViewController.m, you will get a warning saying that you need to implement the delegate function which you already declared. So, set the delegate function, and do the things that you like to do:

    - (void) didUpdateData{
        [self.tableView reloadData];
    }
    

    DON'T forget to set your modelView delegate to self after you instantiate the modalView instance. If not your delegate function won't be fired.

    modalView.delegate = self;