Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason is that the fetched results controller (FRC) has only one section (<strong>section #0</strong>), which you want to be displayed in the second section (<strong>section #1</strong>) of the table view.</p> <p>This is possible, but you have to map between FRC section numbers and table view section numbers, e.g.</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { return 1; } else { NSUInteger frcSection = section - 1; id &lt;NSFetchedResultsSectionInfo&gt; sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:frcSection]; return [sectionInfo numberOfObjects]; } } </code></pre> <p>The same mapping is necessary in <code>cellForRowAtIndexPath</code>.</p> <p>In the FRC delegate methods <code>didChangeObject</code>, <code>didChangeSection</code> you have to <em>add 1</em> to the section number before calling the table view methods (e.g. <code>insertRowsAtIndexPaths</code>).</p> <hr> <p><strong>ADDED:</strong> <code>configureCell</code> should look like this:</p> <pre><code>- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { cell.textLabel.text = entityOne.name; //entityOne object passed from previous VC } else { NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section - 1)]; entityTwo = [self.fetchedResultsController objectAtIndexPath:frcIndexPath]; cell.textLabel.text = entityTwo.name; } } </code></pre> <p>and <code>didChangeObject</code> like this:</p> <pre><code>- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.tableView; NSIndexPath *tvIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section + 1)]; NSIndexPath *tvNewIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row inSection:(newIndexPath.section + 1)]; switch(type) { case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:@[tvNewIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:@[tvIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [self configureCell:[tableView cellForRowAtIndexPath:tvIndexPath] atIndexPath:tvIndexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:@[tvIndexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:@[tvNewIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } } </code></pre> <p>You probably get the idea:</p> <ul> <li>When going from FRC index path to table view index path, add one to the section.</li> <li>When going from table view index path to FRC index path, subtract one from the section.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload