1

I'm so close... but must be missing something.

My local DB has a list of golf courses with various details including among other things, the State, first letter of the state's name (A, C, D, ) etc... The NSFetchedResultsController is grabbing a list of courses and is (or was) working great showing all the states that that had a course I have records for.

At any rate... the section heads seem to be working... but my states are now being duplicated for the number of courses that each given state has.

Code and screen shot below. What am I missing?!? It has to be something so obvious.

enter image description here

-(NSFetchedResultsController *)fetchedResultsController {
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Courses"
                            inManagedObjectContext:_managedObjectContext];

    request.entity = entity;

    request.sortDescriptors = [NSArray arrayWithObject:
                               [NSSortDescriptor
                                sortDescriptorWithKey:@"locState"
                                ascending:YES
                            selector:@selector(caseInsensitiveCompare:)]];


    request.returnsDistinctResults = YES;
    request.fetchBatchSize = 20;

    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
                                       initWithFetchRequest:request
                            managedObjectContext:self.managedObjectContext
                                sectionNameKeyPath:@"locStateSectionHead"
                                       cacheName:nil];
    frc.delegate = self;

    NSError *error = nil;
    if (![frc performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    self.fetchedResultsController = frc;
    return _fetchedResultsController;
}

And then the rest of the tableView setup:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {

    if ([[_fetchedResultsController sections] count] > 0) {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];
    } else {
        return 0;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [self.fetchedResultsController sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"stateCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    // Configure the cell...
    Courses *course_info = [_fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = course_info.locState;

    return cell;
}
Drew
  • 1,422
  • 1
  • 18
  • 29
  • So you want sections to be the first letters, and the rows to be the states, eg first section is "A", with 4 rows, "Alabama", "Alaska", "Arizona", "Arkansas"? Presumably when you tap a cell it will then show a new table view with all the courses for the selected state? – pbasdf May 13 '16 at 15:30
  • Correct. That's how it's been in the past, albeit without the section headers of course. – Drew May 13 '16 at 18:45

1 Answers1

1

Assuming you want your table view to display the states then your NSFetchedResultsController should be fetching the State not the Course. You are getting duplication because you are sorting from courses and you have multiple courses in a state.

Configure your NSFetchedResultsController to load the State entity then your table view will display properly. From there when a user selects a state you can use the relationship from State to Course to display your next scene.

You simply have it backwards :)

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • Duh. Of course... I that seems so simple when looking at it that way! Perhaps the previous developer should have split the states out into their own entity. Right now, there is no State entity. Each course has it's own set of details with state being one of the attributes. Thanks Marcus... I'll give that a shot. Ugh -- refactoring. :-p – Drew May 13 '16 at 18:49
  • Yep. Totally easy once I split the states out and stopped looking at all Courses. Thanks for the (completely obvious) answer. :-p – Drew May 14 '16 at 05:39