开发者

using NSSet relationship to populate cellForRow

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 meth开发者_C百科od, 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 Category objects but you are trying to set your rows with Person objects. The Person objects 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 with Category such that the same Person object 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 Person objects in the people relationship of the Category object chosen in the first tableview.

You can attempt to get it working with the current design by trying something like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    Category *sectionCategory=[[fetchedResultsController fetchedObjects] objectAtIndex:indexPath.section];
    NSSortDescriptor *sort=[NSSortDescriptor sortWithKey:@"pname" ascending:NO];
    NSArray *sortedPersons=[sectionCategory.people sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
    Person *rowPerson=[sortedPersons objectAtIndex:indexPath.row];
    cell.textLabel.text = rowPerson.pname;

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 Person objects of the sectionCategory object each time you populate a row.

I strongly recommend a two tableview solution. That is the preferred solution for hierarchal data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜