I'm trying to set up a tableView
like so:
-Family
--Person 1
--Person 2
--Person 3
-Family 2
--Person 1
--Person 2
--Person 3
-Family 3
... etc
I have no problems getting the sections populated with the family names:
- (NSFetchedResultsController *)fetchedResultsController:(NSString *)forEntity sortyBy:(NSString*)sortAttribute sectionName:(NSString *)sectionName {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:forEntity inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:sortAttribute ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:sectionName cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
self.fetchedResultsController.delegate = self;
return fetchedResultsController;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [self.fetchedResultsController.fetchedObjects count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = self.fetchedResultsController.sections[section];
return [[[sectionInfo objects] objectAtIndex:0] name];
}
However filling in the rows containing the persons is proving difficult for some reason.
There is a One-to-many relationship active that provides Family with an NSSet
attribute called "Persons".
How can I return a correct count on the number of persons in that set and then set the cells accordingly with the Persons' data?
I tried altering the numberOfRowsInSection
method in numerous ways but can't seem to find a non-crashing way of accessing the Family. Persons NSSet
here to count them.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = self.fetchedResultsController.sections[section];
return [sectionInfo numberOfObjects];
}
Should I be using something other than an NSSet
or even NSFetchedResultsController
in a whole?
Important is that I will place "2 segmentedControls
" above the tableView
. This will allow some deep filtering but for that to work I need to be able to fill sections and corresponding rows depending on what the user selects to filter on.
For example:
SegCtrl 1: User selects Family, SegCtrl 2: User selects age.
This draws a UITableView
with the available Family names as sections and as rows all the ages of the Persons in that Family.
These two similar answered questions got me rolling for now:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments