Note that there are some explanatory texts on larger screens.

plurals
  1. PONSFetchedResultsController, grouped UITableView, HeaderInSection and NSSortDescriptor
    primarykey
    data
    text
    <p>I have a NSFetchRequest with NSSortDesctriptors attached to a NSFetchedResultController providing data (see table example) for a UITableView which is in grouped mode and displays section headers. </p> <p>I have some problems getting my head around proper sorting to display sections.</p> <p><strong>Data:</strong> </p> <pre><code>+---+----------+------------+----------+ |id | subTitle | groupTitle | distance | +---+----------+------------+----------+ |1 | A | T1 | 1.1 | +---+----------+------------+----------+ |2 | B | T1 | 1.2 | +---+----------+------------+----------+ |3 | C | T1 | 3.0 | +---+----------+------------+----------+ |4 | D | T2 | 1.3 | +---+----------+------------+----------+ |5 | E | T2 | 1.4 | +---+----------+------------+----------+ |6 | F | T3 | 1.5 | +---+----------+------------+----------+ </code></pre> <p><strong>What I have:</strong></p> <p><code>NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"groupTitle" ascending:YES];</code></p> <pre><code>--T1 ------A ------B ------C --T2 ------D ------E --T3 ------F </code></pre> <p><strong>What I want: (sort by distance but group by groupTitle for each following items in same group)</strong></p> <p><code>NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];</code></p> <pre><code>--T1 ------A ------B --T2 ------D ------E --T3 ------F --T1 ------C </code></pre> <p><strong>How could I achieve this behavior?</strong> probably with the viewForHeaderInSection method? What if the section to be grouped is not in the first hierarchy of the <code>[self.fetchedResultsController sections]</code>?</p> <pre><code>//custom header - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { id&lt;NSFetchedResultsSectionInfo&gt; sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; SomeObject *cellEntity = (SomeObject *)[[sectionInfo objects] objectAtIndex:0]; //cellEntity.groupTitle is the title return aView; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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