Refresh NSFetchedResultsController data?

前端 未结 4 839
暗喜
暗喜 2021-02-07 11:48

I can create new managed objects inside my app, in a separate view I have a table view which shows all the objects I have.

When I create a new managed object (in a total

4条回答
  •  忘掉有多难
    2021-02-07 12:34

    Be sure to add the didChangeObject method:

    - (void)controller:(NSFetchedResultsController *)controller
       didChangeObject:(id) anObject
           atIndexPath:(NSIndexPath *)indexPath
         forChangeType:(NSFetchedResultsChangeType)type
          newIndexPath:(NSIndexPath *)newIndexPath 
    {
      switch(type) 
      {
        case NSFetchedResultsChangeInsert:
          [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                                withRowAnimation:UITableViewRowAnimationFade];
          break;
        case NSFetchedResultsChangeDelete:
          [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                withRowAnimation:UITableViewRowAnimationFade];
          break;
        case NSFetchedResultsChangeUpdate:
          [self configureCell:[self.tableView
        cellForRowAtIndexPath:indexPath]
                  atIndexPath:indexPath];
          break;
        case NSFetchedResultsChangeMove:
          [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                withRowAnimation:UITableViewRowAnimationFade];
          [self.tableView insertRowsAtIndexPaths:[NSArray
                                 arrayWithObject:newIndexPath]
                                withRowAnimation:UITableViewRowAnimationFade];
          break;
      }
    }
    
    - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
    {
      NSManagedObject *note = [self.fetchedResultsController objectAtIndexPath:indexPath];
      cell.textLabel.text = [note valueForKey:@"title"];
    }
    

    The new managed object showed up in the table view after this.

提交回复
热议问题