Search code examples
iphonecore-dataios4uisplitviewcontrollerios5

UISplitView Details not updating after Core Data object is saved


I am developing a new iPad UISplitViewController App, I have a MasterView on the left with Core Data objects. When a CoreData object is selected its details are shown in the DetailView. The object's details are able to be edited and changed in the DetailView and then saved by pressing a button in the DetailView.

After the save button is pressed the user has to re-select the Core Data object in the MasterView to see Object's updated information.

I want to reload the Object's data when the save button is pressed.

I attempted to call the MasterView's

- (void)tableView:(UITableView *)aTableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath

but have not been successful at updating the Objects details from the detailView.

Any suggestions or code would be greatly appreciated.


Solution

  • You need to use a NSFetchedResultsController in your master view. Pass the selected core data object to the detail view to be edited. When the save occurs, you can implement automatic callbacks to your table view by taking advantage of the NSFetchedResultsControllerDelegate protocol.

    In the protocol callback, things are quite simple. Here is a complete stub for the didChangeObject callback.

    -(void)controller:(NSFetchedResultsController *)controller 
        didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath 
        forChangeType:(NSFetchedResultsChangeType)type 
        newIndexPath:(NSIndexPath *)newIndexPath {
    
      if (type == NSFetchedResultsChangeDelete) {}
      else if (type == NSFetchedResultsChangeInsert) {}
      else if (type == NSFetchedResultsChangeMove) {}
      else if (type == NSFetchedResultsChangeUpdate) {      
         [self.tableView reloadRowsAtIndexPaths:@[indexPath]
              withRowAnimation:UITableViewRowAnimationAutomatic];
         UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
         [cell setSelected:YES animated:YES];
      }
    }
    

    The selection stuff (last two lines) is usually not required, but sometimes the selection gets lost - use this if you want to retain the visual indication of the selection.