I’ve looked through many posts and still not sure how to resolve this. Hope someone can help.
It works until hitting the last data source method, cellForRow.
At that point I can get the correct NSSet for each section, but unordered. How does intropection into the relationship properties work for the rows?
using a string literal in the cellForRow I do get the correct number of rows in each section, but obviously no connection to the managed objects that would be there.
How can I populate the rows from the NSSet relationship? All insight appreciated
Category<<—>>Person
cName ———- pName
relationships
people ———- categories
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController fetchedObjects] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Category* cat = [[self.fetchedResultsController fetchedObjects] objectAtIndex:section];
return [[cat people] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
Category* cat = [[self.fetchedResultsController fetchedObjects] objectAtIndex:section];
NSNumber *rowCount = [NSNumber numberWithUnsignedInteger:[[cat people] count]];
return cat.cName;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *mo = [[fetchedResultsController fetchedObjects] objectAtIndex:index.row]];
// returns the correct set but unordered, possibly work with this to populate the rows?
//NSSet *theSet = [[NSSet alloc] initWithSet: [mo valueForKeyPath:@"people.pName"]];
// doesn't work
// cell.textLabel.text = [NSString stringWithFormat:@"%@",[mo valueForKeyPath:@"people.pName"]];
cell.textLabel.text = @"something";
return cell;
}
Your problem is that you are fetching
Categoryobjects but you are trying to set your rows withPersonobjects. ThePersonobjects are unordered because they are not the fetched objects. They have no relationship to the logical structure of the tableview. In fact, they can’t because they have a to-many relationship withCategorysuch that the samePersonobject can show up in many times in the same table.The best solution is to decompose this into two hierarchal tables. One displays the Category list and the second displays the
Personobjects in thepeoplerelationship of theCategoryobject chosen in the first tableview.You can attempt to get it working with the current design by trying something like:
This will work if your data is static. If it changes while the table displays, you will have trouble. It will have a bit of overhead because you have to fetch and sort all the
Personobjects of thesectionCategoryobject each time you populate a row.I strongly recommend a two tableview solution. That is the preferred solution for hierarchal data.